Go Back  Mac-Forums.com  > Software > OS X - Darwin and Development > Is implementing autorelease code in constructor a good idea ?

Reply
 
LinkBack Thread Tools
Old 10-13-2009, 03:56 AM   #1 (permalink)
Bracer

 
Bracer's Avatar
 
Member Since: Oct 07, 2009
Location: Singapore
Posts: 14
Bracer is on a distinguished road
Is implementing autorelease code in constructor a good idea ?

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]
.
Bracer is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Stumble this post
Reply With Quote
Old 10-13-2009, 08:45 PM   #2 (permalink)
wmprice1240
Banned
 
Member Since: May 03, 2009
Posts: 481
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
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.
wmprice1240 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Stumble this post
Reply With Quote
Old 10-25-2009, 12:17 PM   #3 (permalink)
ponhugste

 
Member Since: Oct 25, 2009
Posts: 1
ponhugste is on a distinguished road
Good post. I appreciate it.
Many thanks to ur post. I love it.
ponhugste is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Stumble this post
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 On

Forum Jump

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

 
 
 
     
Home Calendar Get New
     

Copyright ©2001-2009 Mac-Forums.com. All Rights Reserved. A division of iNET Interactive.

Other iNET Interactive Sites: Web Hosting Talk | Swish Talk | Hosting Catalog.com| Dev Papers| Deleted Domains| Hot Scripts

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