In my code I create an NSURL object called fromURL in the header file of my application delegate.
NSURL *fromURL;
Here is when I set it:
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:NO];
[openDlg setCanChooseDirectories:YES];
[openDlg setCanCreateDirectories:YES];
[openDlg setPrompt:@"Select"];
if ([openDlg runModal] == NSOKButton )
{
fromURL = [openDlg URL];
}
Here’s my problem. When I set it I can NSLog what it is set to immediately after it is created but the next time I try to get the information from it it says EXC_BAD_ACCESS. I have turned on zombies and it becomes a zombie almost immediately after I have set it.
How is this just getting immediately deallocated?!?
Sounds like you need to read the Memory Management Programming Guide.
What’s going on here is that your
fromURLvariable is an ivar (at least, I assume it’s an ivar, you might have made it a global variable instead). You’re assigning to it in your method. But you’re not dealing with memory management, so when control returns to the run loop and the autorelease pool is drained, yourfromURLivar ends up pointing at a released object. You need to retain and release as appropriate. For this particular method I might useAnd don’t forget to release
fromURLin your-deallocmethod either.This can be simplified a bit if you define a property for your
fromURL, as inThis way you can use
and not have to worry about retaining/releasing except in
-deallocwhere you still need the[fromURL release]