On 2 Dec 2007, at 1:28 AM, Adam R. Maxwell wrote:

> If I compile and run the following code, the ivars in the parent and
> child classes are released.
>
> For some reason I was thinking that the subclass' dealloc wouldn't be
> called.  Since it is called, we don't have to do anything if super
> returns nil...right?  I'm fully confused now.
>

That's correct. In -[Parent init] (called from within -[Child  
init]) , the object is released. Therefore it's -dealloc is called.  
As the object is a Child, this is -[Child dealloc]. So _string is  
released. And it also calls [super dealloc], so also _parentName is  
released. So there never has to be an explicit release of ivars in  
init, cleanup is all taken care of by dealloc (as it should).

Anyway, in practice for us this would never occur, as -[NSObject  
init] is empty and never returns nil.

Christiaan

> #import <Foundation/Foundation.h>
> @interface Parent : NSObject
> {
>      NSString *_parentName;
> }
> @end
>
> @interface Child : Parent
> {
>      NSString *_string;
> }
> @end
>
> @implementation Parent
>
> - (id)init
> {
>      _parentName = [@"parentName" copy];
>      self = [super init];
>      if (self) {
>          [self release];
>          self = nil;
>      }
>      return self;
> }
>
> - (void)dealloc
> {
>      NSLog(@"dealloc parent %@, release _parentName %@", self,
> _parentName);
>      [_parentName release];
>      [super dealloc];
> }
>
> @end
>
> @implementation Child
>
> - (id)init
> {
>      _string = [@"test" copy];
>      unsigned ptr = (unsigned)self;
>      self = [super init];
>      if (nil == self) {
>          NSLog(@"super init failed for 0x%x", ptr);
>      }
>      return self;
> }
>
> - (void)dealloc
> {
>      NSLog(@"dealloc child %@, releasing _string %@", self, _string);
>      [_string release];
>      [super dealloc];
> }
>
> @end
>
> int main (int argc, char const *argv[])
> {
>      NSAutoreleasePool *pool = [NSAutoreleasePool new];
>
>      Child *c = [Child new];
>
>      [pool release];
>      return 0;
> }



-------------------------------------------------------------------------
SF.Net email is sponsored by: The Future of Linux Business White Paper
from Novell.  From the desktop to the data center, Linux is going
mainstream.  Let it simplify your IT future.
http://altfarm.mediaplex.com/ad/ck/8857-50307-18918-4
_______________________________________________
Bibdesk-develop mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-develop

Reply via email to