Re: init and dealloc
Re: init and dealloc
- Subject: Re: init and dealloc
- From: Shawn Erickson <email@hidden>
- Date: Thu, 29 Jan 2004 16:10:39 -0800
On Jan 29, 2004, at 2:09 PM, Lorenzo wrote:
>
Hi,
>
I init a subclass of NSObject this way:
>
>
- (id)initWithData:(NSData*)theData sender:(id)sender
>
{
>
self = [super init];
>
if(!self) return nil;
>
>
if(![self checkData:theData]) return nil;
>
>
owner = sender;
>
>
return self;
>
}
>
>
Now, if I return "nil" because of checkData returns NO,
>
what should I write to the dealloc method? This?
If you return nil because you are aborting your initialization you must
first release yourself before you return otherwise you will leak the
instance you are attempting to initialize.
The following override does nothing. It just "calls" the super's
dealloc method so why bother with the override?
>
- (void)dealloc
>
{
>
[super dealloc];
>
}
This is what I would do...
- (id)initWithData:(NSData*)theData sender:(id)sender
{
if (self = [super init]) {
if (! [self check
Data:theData]) {
[self release]; //need to free ourselves
return nil;
}
//I guess check data saves the data in the instance somehow?
//this retain sounds like it will result cycle based on your
usage
owner = [sender retain];
}
return self;
}
- (void)dealloc
{
[owner release];
//release stuff from data...?
[super dealloc];
}
>
Also, I use all the time the trick of the "sender" when I init some
>
object,
>
so, from the object, I can call the creator of the object. Is this the
>
correct procedure or is there a better way to get the creator of the
>
object?
This in not a normal thing to be doing...
Why does your object have to know its creator? What is the real need?
-Shawn
_______________________________________________
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.