Re: Why do I have to initialize NSError ?
Re: Why do I have to initialize NSError ?
- Subject: Re: Why do I have to initialize NSError ?
- From: Kevin Perry <email@hidden>
- Date: Tue, 8 Mar 2011 08:32:39 -0800
This is a bug in the framework. NSError-returning methods should not need to check the value of the dereferenced pointer before setting it.
Please file a bug for this.
-KP
On Mar 8, 2011, at 7:22 AM, Gerriet M. Denkmann wrote:
> I thought that NSError is just an output parameter, which is set only in case of errors.
>
> But in my completion handler block (see below) NSError MUST be initialized to nil, or it will NOT be set.
>
> The same code works as expected when NOT inside a block.
>
>
> #define REMOVE_BUG // if this is NOT defined, we get:
>
> [...] __-[Alias_TestAppDelegate openAliasFile:]_block_invoke_1 outError1 0x10057f3b0 (NOT initialized)
> [...] __-[Alias_TestAppDelegate openAliasFile:]_block_invoke_1 outError2 0x10057f3b0 (after error return)
> [...] -[NSOpenPanel recoveryAttempter]: unrecognized selector sent to instance 0x10057f3b0
>
>
> Here is the code:
>
> - (IBAction)openAliasFile: sender;
> {
> (void)sender;
>
> NSOpenPanel *openPanel = [ NSOpenPanel openPanel ];
> [ openPanel setAllowsMultipleSelection: NO ];
> [ openPanel setCanChooseDirectories: NO ];
> [ openPanel setResolvesAliases: NO ];
> [ openPanel setMessage: @"Select Alias File" ];
>
> [ openPanel beginSheetModalForWindow: self.window
> completionHandler: ^(NSInteger result)
> {
> if (result == NSOKButton)
> {
> NSURL *bookmarkFileURL = [ openPanel URL ];
>
> [ [ self window ] setTitleWithRepresentedFilename: [ bookmarkFileURL path ] ];
>
> [ openPanel orderOut:self]; // close panel before we might present an error
>
> // if <bookmarkFileURL> is NOT an alias file, <bookmarkData> will be nil, but <outError>
> // is NOT set. Works correctly if we initialise <outError> to nil.
>
> #ifdef REMOVE_BUG
> NSError *outError = nil;
> #else
> NSError *outError;
> NSLog(@"%s outError1 %p (NOT initialized)",__FUNCTION__, outError);
> #endif
>
> NSData *bookmarkData = [ NSURL bookmarkDataWithContentsOfURL: bookmarkFileURL
> error: &outError
> ];
> if ( bookmarkData == nil ) // error
> {
> NSLog(@"%s outError2 %p (after error return)",__FUNCTION__, outError);
>
> [ self presentError: outError
> modalForWindow: [ self window ]
> delegate: nil
> didPresentSelector: NULL
> contextInfo: NULL
> ];
>
> return;
> };
>
> // do something with valid bookmarkData
>
> }
> else // cancelled
> {
> self.dataString = @"-- Cancelled --";
> };
> }
> ];
> }
>
>
> Kind regards,
>
> Gerriet.
>
> P.S. Tested on 10.6.6
>
> _______________________________________________
>
> 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
_______________________________________________
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