tags:

views:

268

answers:

2

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 windows should safely be closed as a result of users interacting with views on them.

+1  A: 

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];
Manjunath
A: 

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.

Peter Hosey