New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Is implementing autorelease code in constructor a good idea ?


Post Reply New Thread Subscribe

 
Thread Tools
Bracer

 
Bracer's Avatar
 
Member Since: Oct 07, 2009
Location: Singapore
Posts: 14
Bracer is on a distinguished road

Bracer is offline
Code:
-(id) init
{
	if (self = [super init]) 
	{
	[self autorelease];
	return self;
	}
}
With all the memory dangers that hides behind the corner and with the knowledge that there are functions that returns autorelease objects like arrayWithCapacity or stringWithFormat, shouldn't it be a good idea to also implement autorelease in our own objects like this code illustrated above for when the pool drains, it will go as well and there is no need to pollute the main codes with layers of
Code:
[object autorelease]
.
QUOTE Thanks
wmprice1240

 
Member Since: May 02, 2009
Posts: 480
wmprice1240 has a spectacular aura about
Mac Specs: MBP 2.33 4GB: MacPro 8 Core 2.8, 16GB: MacMini 2.26 4GB: MacMin 2.53 4GB: iPhone3GS 32GB

wmprice1240 is offline
Typically any well defined abstraction should not try and anticipate or make assumptions about the memory requirements for a particular client, platform, environment etc, and this includes the existence and scope of an autorelease pool.

At this point, your object has yet to be fully initialized so your essentially adding a partially initialized object to the autorelease pool which can have side effects. Similarly, Autorelease pools have a particular scope which is thread dependent. Embedding the autorelease knowledge into the initializer (note, the terms are different in Obj-C, there is no such thing as a 'constructor' in Obj-C) would make certain assumptions that might not be valid in all cases.

Obj-C has well defined semantics,idioms and usage patterns regarding object ownership and memory management.

You will want to read:

Mac Dev Center: Memory Management Programming Guide for Cocoa: Introduction

If code clarity is your concern you may want to consider using the garbage collector that is now a part of Objective-C 2.0 if your platform (the iPhone does not support the GC) permits its use.
QUOTE Thanks
ponhugste

 
Member Since: Oct 25, 2009
Posts: 1
ponhugste is on a distinguished road

ponhugste is offline
Good post. I appreciate it.
Many thanks to ur post. I love it.
QUOTE Thanks
htvngoc2010

 
Member Since: Jan 06, 2010
Posts: 1
htvngoc2010 is on a distinguished road

htvngoc2010 is offline
Quote:
Originally Posted by wmprice1240 View Post
Typically any well defined abstraction should not try and anticipate or make assumptions about the memory requirements for a particular client, platform, environment etc, and this includes the existence and scope of an autorelease pool.

At this point, your object has yet to be fully initialized so your essentially adding a partially initialized object to the autorelease pool which can have side effects. Similarly, Autorelease pools have a particular scope which is thread dependent. Embedding the autorelease knowledge into the initializer (note, the terms are different in Obj-C, there is no such thing as a 'constructor' in Obj-C) would make certain assumptions that might not be valid in all cases.

Obj-C has well defined semantics,idioms and usage patterns regarding object ownership and memory management.

You will want to read:

Mac Dev Center: Memory Management Programming Guide for Cocoa: Introduction

If code clarity is your concern you may want to consider using the garbage collector that is now a part of Objective-C 2.0 if your platform (the iPhone does not support the GC) permits its use.
^^ good point. Glad I joined this forum already
QUOTE Thanks

Post Reply New Thread Subscribe


« Why can't i get gimp to install? | Objective-C and the Interface section »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Good idea? or bad? lilfordbmx Switcher Hangout 0 06-14-2006 12:28 AM
Bought TiBook... good idea? Aptmunich Apple Notebooks 3 08-01-2005 06:08 AM
More than 2 Threads on dual G5...good idea? MadSkillzMan Apple Desktops 2 07-19-2005 12:35 AM
Good Idea? awu_gigabyte Switcher Hangout 8 02-21-2005 11:28 AM
A good idea! )(* Schweb's Lounge 4 01-17-2005 10:42 PM

All times are GMT -4. The time now is 01:18 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?