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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to