开发者

Closing an NSWindow

I have a NSWindow that hosts a WebView that Ive hooked up to a script handler.

Now, when the user clicks a button on a control on the WebView it calls a Objective C method on my object.

In this specific case, the action of the button is to try and close the window hosting the WebView

[[webView window] close];

This usually works, but sometimes i get a SEGFAULT or some other access violation as a result of the event loop trying to dispatcha mouse message to the now destroyed view.

The callstack is horrible when I try to close the window, the even loop has called the window has called the webView, has called my script delegate when I try and close the window. Destruction of an object from a callback from that object is generally, well, dangerous, but I can't figure out how 开发者_如何学Gowindows should safely be closed as a result of users interacting with views on them.


Istead of closing, can't you try out the API

- (void)orderOut:(id)sender

just check whether your window is visible and orderout that window

if([[webView window] isVisible])
   [[webView window] orderOut:self];


The callstack is horrible when I try to close the window, the even loop has called the window has called the webView, has called my script delegate when I try and close the window. Destruction of an object from a callback from that object is generally, well, dangerous, but I can't figure out how windows should safely be closed as a result of users interacting with views on them.

You can use performSelector:withObject:afterDelay: to put off closing the window until 0.0 seconds after the button hit.

In this specific case, the action of the button is to try and close the window hosting the WebView

[[webView window] close];

This usually works, but sometimes i get a SEGFAULT or some other access violation as a result of the event loop trying to dispatcha mouse message to the now destroyed view.

That's not likely. The event loop will only dispatch an event for a window that exists; if you have closed and thereby destroyed a window, no event can arrive at that window, nor at any view that may once have been in it.

It would help if you would edit your question to include the stack trace for that crash.


I know this is an old question, but I'm having a similar issue and I suspect the accepted answer is missing the point. I believe the window hosting the WebView is still one or more of the delegates of the WebView, and delegate methods are being called after the WebView finishes loading, which is after the window is closed.

I was looking for the right way to resolve this… I'll keep looking. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜