On Aug 30, 2010, at 6:37 PM, Dave Geering wrote:

>> // 1)
>> self.serialIDs = [[IRMSerialDetailsDO alloc] init];
> 
> The alloc method allocates an instance with a retain count of 1, and
> assigning it to the serialIDs property bumps it up to 2. In your
> dealloc method, you will [hopefully] send it a release message which
> puts it back at 1, but this means the object still survives (i.e. it
> is not deallocated).
> 
>> // 2)
>> IRMSerialDetailsDO *mySerialIDDO = [[IRMSerialDetailsDO alloc] init];
>> self.serialIDDO = mySerialIDDO;
>> [mySerialIDDO release];
> 
> Here, the alloc creates the instance with a retain count 1, assigning
> it to the property bumps it up to 2, and the release right afterwards
> puts it back down to 1. In your dealloc method, you will sent it
> another release message which puts it at 0, and therefore the instance
> is deallocated.
> 


This is at best misleading, and illustrates why people are typically 
discouraged from reformulating the memory management rules.

First, from the original post:
> <header file>.h

> @property(nonatomic, release) IRMSerialDetailsDO *serialIDs;
> ...
> <body>.m
> @synthesize mySerialIDDO
> 
This is just wrong.
There is no 'release' keyword for a property declaration, and the name of the 
property in the declaration does not match that in the synthesize statement.

Assuming that the intent was:

<header file>.h
@property(nonatomic, retain) IRMSerialDetailsDO * mySerialIDDO;
...
<body>.m
@synthesize mySerialIDDO


then if you set the mySerialIDDO variable using the corresponding accessor 
method, you end up owning the object. It is your responsibility to subsequently 
relinquish ownership in dealloc. Whether or not this results in the object 
being deallocated depends on what other objects may have claimed ownership in 
the interim, and is of no interest to you.

As others have noted, adhering to the memory management rules 
(<http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html>),
 by convention this results in a memory leak:

self.serialIDs = [[IRMSerialDetailsDO alloc] init];

To address the issue of the second pattern:

> IRMSerialDetailsDO *mySerialIDDO = [[IRMSerialDetailsDO alloc] init];
> self.serialIDDO = mySerialIDDO;
> [mySerialIDDO release];
> 

This is explained in the Your First iOS Application tutorial:
        
<http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhone101/Articles/03_AddingViewController.html#//apple_ref/doc/uid/TP40007514-CH5-SW1>

It is, and remains, best practice to avoid using autorelease where possible. 
Autoreleasing objects means they may end up living longer than is strictly 
necessary, and this adds to your application's memory footprint. Not something 
you want on a deive with a constrained amount of memory.

mmalc










_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to