Pergunta

OK, the situation is a bit complicated but the nature of the issue itself should be pretty simple for anyone more familiar than me with me Cocoa memory management.

A few details : - From my main app, I'm loading a "loadable" bundle (which consists of an NSWindowController) - Both projects (the main app and the "plugin") are ARC-enabled

In my plugin code, I have a custom initialization method :

- (id)initWithAPI:(id)api
{
    pluginWindowController* newPluginWindowController = [super initWithWindowNibName:@"PluginWindow"];

    if (newPluginWindowController)
    {
        [newPluginWindowController setAPI:api];
    }

    return newPluginWindowController;

}

This is how API is declared :

@property (unsafe_unretained) id API;

Now, here's the catch :

  • I'm initializing using initWithAPI: and API is being set.
  • When I'm testing API's value in - (void)windowDidLoad: it still shows up fine.
  • However : when trying to do the very same thing from within another method (actually an IBAction triggered by a button click in my plugin's window)... trying to get [self API] triggers an EXC_BAD_ACCESS error.

What am I doing wrong? Any ideas?

Foi útil?

Solução

Either declare the property as weak, if you want it to automatically be set to nil when the object dies, or as strong, if you want to keep the object alive (strong is probably what you want).

unsafe_unretained will result in a dangling pointer if the object is deallocated (that's why its called "unsafe"..)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top