zURL is declared like:
.h:
@interface PanelController : NSWindowController <NSWindowDelegate>
{
NSURL *zURL;
}
@property (nonatomic, copy) NSURL *zURL;
.m:
@synthesize zURL;
In one IBAction I have:
- (IBAction)openBrowser:(id)sender {
NSOpenPanel *zOpenPanel = [NSOpenPanel openPanel];
[zOpenPanel setCanChooseFiles: TRUE];
[zOpenPanel setCanChooseDirectories: TRUE];
[zOpenPanel setAllowsMultipleSelection: TRUE];
[zOpenPanel setLevel:CGShieldingWindowLevel()];
NSInteger zIntResult = [zOpenPanel runModal];
if (zIntResult == NSFileHandlingPanelCancelButton) {
return;
}
// zURL set here
zURL = [NSURL alloc];
zURL = [zOpenPanel URL];
NSLog(@"url = %@", zURL); // works
NSString *zStr = [zURL absoluteString];
_fileField.stringValue = zStr;
[_importButton setEnabled:TRUE];
[self openPanel];
}
In the next IBAction:
NSLog(@"url = %@", zURL); // EXC_BAD_ACCESS error
You are assigning your URL to
zOpenPanel‘s URL attribute, but not indicating you intend to keep the reference by retaining it.your second line replaces your reference with a reference to
zOpenPanel‘s URL attribute, leaking the URL you just created above.at some point later,
zOpenPaneltells the OS it is done with its URL, and frees the ram thus invalidating your reference.what you’re probably trying to do:
this assigns your reference to
zOpenPanel‘s URL attribute and tells the OS you want partial ownership of it’s lifetime.when you are done with zURL you will need to release it and relinquish your claim on it’s lifetime:
edit:
since you’re declaring it as a property on your class, you should use the property notation to invoke the appropriate behavior:
since you’ve declared the property to have copy semantics, this will copy the URL, and you will still need to release it in your
dealloc.