Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView memory leak in windowScriptObject setValue:self forKey:?

I have a Cocoa app with an embedded WebView. I set the window controller as accessible through JavaScript with the code below.

The problem is that the call to setValue:self forKey:@"console" seems to introduce a memory leak under ARC in a sense that the WebView and window controller are never released after the window was closed.

When I comment out the setValue line the WebView and controller are automatically released when the window is closed.

I tried [self.webView.windowScriptObject removeWebScriptKey:@"console"], but that did not work either.

Code to add the JavaScript object in the window controller:

- (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowObject
       forFrame:(WebFrame *)frame {

    if (frame != self.webView.mainFrame) return;

    [self.webView.windowScriptObject setValue:self forKey:WELCOME_WINDOW_JS_NAME];
}
like image 428
Mark Avatar asked Mar 22 '26 12:03

Mark


1 Answers

Mark,

You have identified the problem code. What is missing is that you aren't setting the key to nil when you release the window: setValue: nil forKey: @"console". Do that and all will be well.

Andrew

like image 146
adonoho Avatar answered Mar 24 '26 12:03

adonoho