Is implementing autorelease code in constructor a good idea ?

Joined
Oct 7, 2009
Messages
14
Reaction score
0
Points
1
Location
Singapore
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]
.
 
Joined
May 2, 2009
Messages
480
Reaction score
13
Points
18
Your Mac's Specs
MBP 2.33 4GB: MacPro 8 Core 2.8, 16GB: MacMini 2.26 4GB: MacMin 2.53 4GB: iPhone3GS 32GB
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.
 
Joined
Jan 6, 2010
Messages
1
Reaction score
0
Points
1
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
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top