On Oct 19, 2009, at 23:35, Nick Rogers wrote:

Since the methods -release, -retain and -autorelease are no-ops with GC only enabled, How do I release everything associated with an ivar (pointer to a class) in AppController?
Currently I'm doing it by setting this pointer as nil. Is it ok?

Also when I follow certain steps, this pointer gets a lot of memory attached, down the line. which gets released (when I set it to nil) as per Obj-Alloc instrument. But when following another certain steps, this memory doesn't get released (when setting this pointer nil), why? With GC only enabled this should get freed up.

You should read -- several times probably -- the documentation on the garbage collector:

        
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/GarbageCollection/Introduction.html

That will tell you that you don't have to do anything to 'release' memory under GC -- memory is collected when there are no "live" references to it. So if your pointer is *the* reference that's keeping the memory alive (along with everything indirectly pointed to), then yes setting the pointer to nil should result in the whole shebang be collected -- at some later time that you don't directly control.

OTOH, if the ivar is in an instance which is itself unreferenced, setting the ivar to nil is unnecessary because it's no longer a live reference. A typical usage scenario of this is when the pointer to the instance is in a local (stack) variable of a method, and you return from the method. There's no need to nil anything in that case, because the return ends the lifetime of the instance, which the ends the lifetime of the object pointed to by the ivar, which ends ... etc.


_______________________________________________

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