Here's the situation. Let's say I have a class called MYFoo. Here's it's initializer:
-init
{
self = [super init];
if (self)
{
// during initialization, something goes wrong and an exception is raised
[NSException raise ...];
}
return self;
}
Now somewhere else I want to use a MYFoo object, so I use a common pattern:
MYFoo *foo = [[[MYFoo alloc] init] autorelease];
But what's going to happen is that, even if there's a try/catch around the 2nd part, a MYFoo object is going to be allocated, the exception will be thrown, the autorelease missed, and the uninitialized MYFoo object will leak.
What should happen here to prevent this leak?
nil
. Try putting your dangerous operations in separate methods and using a combination of signaling andNSError
to deal with failure. – shosti Feb 15 '11 at 16:04