views:

219

answers:

2

When you release an object in Objective-C (assuming its release count is 1) its release count is decremented to 0 and the dealloc method called. Is the object destroyed right there and then after the [super dealloc], or is it added to the pool and destroyed when the pool is drained?

I would assume that released objects are destroyed at the end of dealloc (when [super dealloc] is called) I know autorelease variables are added to the pool, just wanted to be sure what happens to normal released objects.

cheers -gary-

+5  A: 

Yes, they are deallocated as soon as the retain-count hits zero.

The autorelease system is for objects who's ownership is a little "ambiguous" - i.e. when you're returning a fresh object that you don't want to own, whose lifetime is unknown, and you don't want to assume the caller will take responsibility for. When the autorelease pool is drained (generally next time around the run-loop), all members are sent release. If the caller who received your object wants to take responsibility for it, all it need do is retain it, and this will avoid its deallocation.

Adam Wright
So [object release]; Release count set to 0; dealloc called (to release any instance variables); object deallocated; .... does that sound right?
fuzzygoat
Yup, sounds right to me. Of course, custom dealloc implementations can do whatever they like.
Adam Wright
The current threads autorelease pool is drained at the end of the current run-loop, not the next run. This is important as otherwise this thread could keep memory for a very long time while waiting for timers, user events and other run-loop sources.
PeyloW
+6  A: 

First off, Objective-C the programming language does not have any concept of memory management. Memory management is built into Foundation (the common framework of Cocoa for Mac OS X and Cocoa Touch on iPhone OS). Foundation adds a rootclass NSObject that implements alloc, retain, release and autorelease as convenience wrappers on top of class_createInstance() and object_dispose() functions from the Objective-C run-time.

Since Objective-C is memory management agnostic, adding garbage collection and making all memory management methods on NSObject no-ops was quite easy. But there is no garbage collection on iPhone OS and legacy Mac OS X and there we instead use a reference counting scheme in Cocoa.

An object is created when calling the alloc class method on NSObject or NSProxy from Foundation. These default implementations will call class_createInstance() so that you never need to manually.

An object "dies" when dealloc is run on the root class NSObject. This is when the memory for the object on the heap is released by calling object_dispose(), you will never need to call this function yourself as long as you inherit from NSObject or NSProxy from Foundation.

Autoreleased objects are not treated any special as far as the run-time is concerned, an autoreleased object is just as alive as any other object. What happens when you autorelease an object is approximately;

-(id)autorelease; {
  [NSAutoreleasePool addObject:self];  // retain count +1
  [self release];                      // retain count -1
  return self;
}

Calling autorelease will not decrement the retain count, it will just transfer ownership of the object from the caller to the current autorelease pool. Later when the current autorelease pool goes away, it will call release on all of the objects it owns, and any object no longer owned by anything else is released.

PeyloW
So in Objective-C is there always a dealloc method even if a custom one is not defined. That would seem to make sense, release object, retain count goes to 0, dealloc called, [super dealloc] called, object dead?
fuzzygoat
Yes and No. I have edited my answer to clarify the difference between Objective-C and Cocoa.
PeyloW