alloc, init and autorelease/release
alloc, init and autorelease/release
- Subject: alloc, init and autorelease/release
- From: Felix Schwarz <email@hidden>
- Date: Sat, 22 Oct 2005 15:32:50 +0200
Hi everyone,
I just have a small question. Which of those implementations of init
code are valid code?
Code sample 1:
- (id) init
{
self = [super init];
if (someErrorCondition)
{
[self autorelease];
return (nil);
}
return (self);
}
Code sample 2:
- (id) init
{
if (someErrorCondition)
{
[self autorelease];
return (nil);
}
else
{
// Calculation of someParameter goes here ..
return ([self someOtherInit:someParameter]);
}
}
Or, in other words, is it ok to autorelease and or release an object
right after "alloc"ation, before it was "init"ialized?
I.e. is it ok to do
id someObj = [SomeObj alloc];
[someObj release];
or
id someObj = [SomeObj alloc];
[someObj autorelease];
or does it *have to* be
id someObj = [[SomeObj alloc] init];
[someObj release];
to be error-free?
Best regards,
Felix
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden