Proper shutdown of AudioDeviceManager


I have an app that opens an audio device and plays music beautifully (thanks for the help so far!). My problem has to do with a crash on shutdown.

I call the following code in my application shutdown:

  gDeviceManager->removeAudioCallback( this );
  delete gDeviceManager;

Unfortunately the app crashes, because despite these calls, the audio thread is still working within audioDeviceIOCallbackInt() in Juce. In other words, none of the above functions (removeAudioCallback() or closeAudioDevice()) wait for the callbacks to end or the device to close before returning.

Is there a proper method to shut down the AudioDeviceManager the will guarantee that processing is completed? I’m sure I could put a Sleep() in to prevent crashes, but that’s really more of a band-aid.


No, the callback will definitely not be called after the removeAudioCallback method returns. You must be misunderstanding the problem.

And I’m so tired of seeing so many code snippets on the forum that still don’t use smart-pointers. Seriously, people, come on! Even if you use “delete” in the privacy of your own home, please don’t do it in public - there might be impressionable beginners who see your code and assume that it’s acceptable coding style.

Thank you. Armed with that knowledge I was able to figure out the problem.

Regarding smart pointers, my apologies as I know you’ve been doing this for some time, but for those of us that are new to JUCE it can be harder to know what the best practices are. I can’t seem to find a tutorial on best practices for using smart pointers. I’ll search the forums but if you can point at a thread or page with more information that would be helpful.


This has less to do with JUCE and more to do with modern C++ best practices.

Thank you, “TheVinn”, for that judgmental and unhelpful answer. As it happens, I’m perfectly familiar with using scoped pointers and shared pointers in application development. Those of us that are using JUCE to write cross-platform, multi-platform applications however can’t rely on a single standard for using smart pointers, which is why I’d like to use JUCE’s pointer system, and use it properly.


Sorry if I sounded a bit snappy about smart-pointers. It’s just getting frustrating to see how much old-style code gets posted on the forum!

There’s a bit about smart-pointers here:

But really, any C++ book, tutorial, etc will tell you the same thing. The TL;DR is just: Do not use “delete”!