• 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
NSError and proper cleanup
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSError and proper cleanup


  • Subject: NSError and proper cleanup
  • From: Ryan Homer <email@hidden>
  • Date: Tue, 27 Nov 2007 20:35:56 -0500

Hi everyone,

I'm having some trouble fully understanding how to handle errors using NSError and the Error-Responder chain. I have an application whose main window is in MainMenu.nib. Most of the interaction with this window is handled by an NSObject subclass called MainWindowController which is instantiated in MainMenu.nib. For example, I have a menu option that brings up a window which displays images from iSight. Note that references to a SequenceGrabber are based on that provided in WhackedTV at http://developer.apple.com/samplecode/WhackedTV.

So, choosing a particular menu option will send a debugTestWebcamWindow: message, shown below.

MainWindowController.m
static TestCamWindowController *tcwc = nil;

-(void)debugTestWebcamWindow:(id)sender
{
if(!tcwc)
tcwc = [[TestCamWindowController alloc] initWithWindowNibName:@"TestCam"];

[tcwc startVideo];
}


which will in turn send an initWithWindowNibName: message, shown below.

TestWindowCamController.m (subclass of NSWindowController)
- (id)initWithWindowNibName:(NSString *)windowNibName {
	self = [super initWithWindowNibName:windowNibName];
	if (self != nil) {
		...
	}
	return self;
}

Then awakeFromNib:

- (void)awakeFromNib
{
	.
	.
	.

	sgController = [[SGController alloc] init];

NSError *error = nil;
BOOL success = [sgController addVideoTrackToPreview:preview scaleToFit:YES error:&error];
if(!success && error) {

[sgController release];
sgController = nil;

[self presentError:error];
}
}



Now, let's suppose that addVideoTrackToPreview:scaleToFit:error: fails, say, because we're trying to acquire the camera, but it's in use by another application - or for some other reason. At this point, I'd like to inform the user that there is a problem and clean up any memory that was allocated but will no longer be used.


Right away, I can [sgController release], no problem there. But I would still like to release tcwc (TestCamWindowController). So, I [... presentError:error] to start sending the error up the Error-Responder chain.

This sends willPresentError: to self (there is none in this case) and then sends presentError: to its superview (NSWindowController?) and so on up the chain.

After this, it goes to the Application's delegate, which for my application is NOT MainWindowController (I have another class, AppDelegate).

So, where is my [tcwc release] supposed to go? Was I supposed to include a pointer to tcwc in my NSError object so that I can eventually release it in some unrelated view, window controller or application delegate? This doesn't make sense to me.

Using exceptions, I would have cleaned up my mess right in debugTestWebcamWindow:. So I must be missing something conceptually. I've read the Error Handling Programming Guide for Cocoa several times now and still do not get it. Is it me or is that guide missing something key? :-)

_______________________________________________

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


  • Prev by Date: Re: [NSTableView] option + click change of behavior between Tiger and Leopard
  • Next by Date: Re: Archived QTMovieViews on 64-bit?
  • Previous by thread: Re: validate custom view toolbar item
  • Next by thread: Use of assign vs. copy for accessors in Garbage Collected app
  • Index(es):
    • Date
    • Thread