Results 1 to 4 of 4
  1. #1
    Is implementing autorelease code in constructor a good idea ?
    Bracer's Avatar
    Member Since
    Oct 07, 2009
    Location
    Singapore
    Posts
    14
    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]
    .

  2. #2
    Is implementing autorelease code in constructor a good idea ?

    Member Since
    May 02, 2009
    Posts
    480
    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.

  3. #3

  4. #4
    Is implementing autorelease code in constructor a good idea ?

    Member Since
    Jan 06, 2010
    Posts
    1
    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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Lion OSX on Macbook 2.0Ghz - good idea or bad idea?
    By OM2 in forum OS X - Operating System
    Replies: 29
    Last Post: 05-24-2012, 07:32 PM
  2. Is this a good idea
    By Stradguy93 in forum Apple Notebooks
    Replies: 2
    Last Post: 01-26-2012, 05:19 PM
  3. Is this a good idea?
    By gizmo8899 in forum Other Hardware and Peripherals
    Replies: 18
    Last Post: 12-30-2010, 07:58 PM
  4. you think this is a good idea?
    By DVaris223 in forum Schweb's Lounge
    Replies: 4
    Last Post: 03-23-2007, 07:55 PM
  5. A good idea!
    By )(* in forum Schweb's Lounge
    Replies: 4
    Last Post: 01-17-2005, 10:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •