Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Memory cleanup when init fails?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory cleanup when init fails?



On Sep 23, 2008, at 10:28 AM, Karan, Cem (Civ, ARL/CISD) wrote:
--- Foo.m ---
@implementation
- (id) init
{
        self = [super init];
        if (self != nil)
        {
                // Fake a failure
                [self release]; // or should I call dealloc???
                return nil;
        }
        return self;
}
@end
--- End of Foo.m ---
Is the above correct form to prevent leaking?

You should first clean up anything you already did in your -init, then call [super dealloc]. [self dealloc] or [self release] are bad because they might call some subclass's -dealloc method even though the subclass's -init hasn't done anything yet.



-- Greg Parker email@hidden Runtime Wrangler


_______________________________________________ Do not post admin requests to the list. They will be ignored. Objc-language mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Memory cleanup when init fails? (From: "Karan, Cem (Civ, ARL/CISD)" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.