Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Accessor methods and (auto)release <Memory trail>



There seems to be a strange competition in all the documents on how to
implement accessor methods.

- (void) setValue:(Something *)newValue {
[value autorelease];
value = [newValue retain]; // or copy, OK.
}

seems to be the most current.

However, I read that :
"autoreleasing an object is an expensive operation" etc...
(http://www.stepwise.com/Articles/Technical/MemoryManagement.html)

All right then.
But am I sure that the instance variable is not used elsewhere ?
Because in that case I have to autorelease !

Then, the author proposes this code :
- (void) setTheory: (Theory *)newTheory
{
Theory *oldTheory = nil;

if ( theory != newTheory ) // If they're the same, do
nothing
{
[self willChange]; // For Enterprise Objects only
oldTheory = theory; // Copy the reference
theory = [newTheory retain];// First retain the new object
[oldTheory release]; // Then release the old object
}

return;
}

But this appears to be very strange.
Testing this object avoid to do this for none, and allow the use of
release, all that for optimisation.
All right.
But I understand less the use of a swap variable. This code would be
more simple :

- (void) setTheory:(Theory *)newTheory {
if (theory == newTheory)
return;

// [self willChange]; if necessary (I know nothing about that).
// but that's perhaps the reason of a so complicated manner ?

[theory release];
theory = [newtheory copy/retain];
}

So, what to do ?

R.D.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.