Cleanup inside a failed init method
Cleanup inside a failed init method
- Subject: Cleanup inside a failed init method
- From: Ken Tozier <email@hidden>
- Date: Sat, 6 Dec 2008 16:07:00 -0500
Hi
I'm writing my own socket class using a bunch of BSD functions and am
a little unclear on exactly what I should be doing to insure
everything is cleaned up if any of the low level functions fail. If I
return nil from my init, does the system call my dealloc method to
allow proper cleanup? Or do I have to do the cleanup before returning?
Here's what I have so far
- (id) initWithHost:(NSString *) inHost
port:(int) inPort
error:(NSString **) inError
{
id result = nil;
self = [super init];
if (self)
{
host = [inHost copy];
port = inPort;
// if any of the following fails, I want to cleanup
// completely before returning
// am I doing that correctly?
// "host" and "error" are the only Cocoa objects
// I'm holding on to. The other stuff is from the BSD functions
if ([self getAddress] &&
[self createSocket] &&
[self connect] &&
[self updateStream])
{
result = self;
}
else
{
freeaddrinfo(addressInfo);
close(socketFD);
*inError = [error copy];
[error release];
[host release];
}
}
return result;
}
_______________________________________________
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