Singleton but reference counted?

I want to have a single instance of a GUI class for multiple instances of other classes. I.e. 1 x GUI, 100 x ClassX.

Each ClassX instance should have a pointer to the same instance of the GUI. For this I used JUCE_DECLARE_SINGLETON in the GUI.

ClassX constructor calls the GUI::getInstance() to create/get the singleton.
ClassX destructor calls the GUI::deleteInstance() to get rid of the singleton.

Everything seems to go fine until I delete one of the ClassX instances to create a new one. The singleton’s deleteInstance() simply deletes the GUI, instead of reference counting it. So now all ClassX instances try to access deleted GUI.

Is there some helper macro/method in JUCE which makes it easy to achieve this “reference counted singleton”?

See the SharedResourcePointer as a potential alternative, I think it does exactly what you want.

1 Like

Well, that was a quick and easy fix. Thank you! :slight_smile:
Works!

1 Like