Re: Memory leak if alloc succeeds but init fails?
Re: Memory leak if alloc succeeds but init fails?
- Subject: Re: Memory leak if alloc succeeds but init fails?
- From: Half Activist <email@hidden>
- Date: Fri, 28 Sep 2007 08:06:40 +0200
Yes if you do not issue a release then the memory leaks.
And yes if your [super init] call returns nil, then, whatever the
calss is it's supposed to have cleaned up what it did.
Depending on what does your dealloc method, it should check that what
it frees has been alloced or initialized.
Regards.
On Sep 28, 2007, at 7:38 AM, R.L. Grigg wrote:
On Sep 27, 2007, at 10:12 PM, Chris Suter wrote:
On 28/09/2007, at 3:10 PM, R.L. Grigg wrote:
Apple's Cocoa Memory Management Programming guide shows this
pattern for init:
- (id)init {
if ((self = [super init])) {// superclass may return nil
// your initialization code goes here
}
return self;
}
Newb question: In the case of
id myObject = [[MyObject alloc]init];
what happens to the allocated memory if the [super init] fails
and how do I handle it since I dont have a self to release?
If [super init] fails it should have done [self release] before
returning nil.
Okay so somewhere along the chain up to NSObject, some class in the
hierarchy is responsible for issuing [self release] within it's
aborted init. But just theoretically if it does go all the way up
to NSObject which fails its init, is it's alloced memory leaked?
Not that it will ever happen...
Russ
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden