Is there a way to post a message to the message thread for later execution, after the current callback returns?
MessageManager::callFunctionOnMessageThread() allows worker threads to post messages to the message thread, but when used from the message thread, it’s just a call.
Here is a situation I don’t know how to deal with without the ability to post:
I’ve got a DocumentWindow with some controls on it, including a checkbox.
When I press Alt+F4, the window closes, no matter what control has the focus. This is accomplished by having the DocumentWindow react to the keypress.
Now when I press Alt+F4 while the focus is on a checkbox, the keyboard event is initially processed by Button::keyStateChanged and from there it gets passed to the parent windows, I don’t quite understand what happens, but in VC9 debug mode a danglig pointer exception is produced in the last line of Button::keyStateChanged() because the window and all its components was deleted by a parent’s reaction to the keypress - even though the parent returns true to indicate it processed the keypress.
Juce programs with a single or few windows won’t have this problem because window deletion is done on processing the quit message. But for programs that can create an arbitrary number of windows, a method of delayed deletion would make things a lot easier. The ability to generically post a function call asynchroneously is the most generic way I can think of, and since the posting mechanism is already there for cross-thread communication, I figure it wouldn’t be much work. The problems caused by deleting a window in direct response to an event of one of its controls are rather nasty to track. You could stuff all the code with deletion checkers and still miss the crucial one…