• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Cleanup inside a failed init method
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Cleanup inside a failed init method
      • From: "John Engelhart" <email@hidden>
    • Re: Cleanup inside a failed init method
      • From: mmalcolm crawford <email@hidden>
    • Re: Cleanup inside a failed init method
      • From: Steven W Riggins <email@hidden>
    • Re: Cleanup inside a failed init method
      • From: Charles Steinman <email@hidden>
  • Prev by Date: Re: What are the limitations of Dot Syntax?
  • Next by Date: Re: Cleanup inside a failed init method
  • Previous by thread: Re: NSImageView gets stuck
  • Next by thread: Re: Cleanup inside a failed init method
  • Index(es):
    • Date
    • Thread