Juce shutdown and the message system queue


#1

Hi there!

I have a ChangeBroadcaster that is posting a message to the system queue. In so doing, I see that the reference count of the MessageManager::MessageBase* object is explicitly incremented
(@see juce_wine32_Messaging.cpp: bool MessageManager::postMessageToSystemQueue).

I also see that once the message is dispatched, the reference count is explicitly decremented.
(@see juce_wine32_Messaging.cpp: void WindowsMessageHelpers::dispatchMessageFromLParam).

Great!

I’ve run into a problem whereby my application shutdown logic posts a couple of messages to the system queue (via the ChangeBroadcaster), but the messages are not dispatched (and hence the reference counts are not decremented) before application shutdown. This leads to a tiny memory leak on shutdown. I’ve currently worked around the issue by explicitly preventing specific change broadcast messages from being sent from my application shutdown logic, but this feels hacky.

How can I delay Juce shutdown until all messages from the system queue have been dispatched !? We are currently making use of ScopedJuceInitialiser_GUI to Init and Shutdown Juce from main, or is there another approach I should be using?!

Thank you !!


#2

There’s another approach. Instead of shutting down directly, queue a message into the system queue which shuts the application down. Since the system queue is first in first out, your “quit” message will execute only after everything else has been processed.