Singleton crash when application exit

Here is my singleton testing code:

class TestSingle {
public :
JUCE_DECLARE_SINGLETON(TestSingle, true)
TestSingle(){}
virtual ~TestSingle(){};
ValueTree abc{ “test” };
void test(){ …};
};

JUCE_IMPLEMENT_SINGLETON(TestSingle)

so in my maincode:

mainWindow::mainWindow(){
Testsingle::getInstance()->test();
}

run is ok,and when close window ,it shows leak of object count;

Taking a look at the docs should help: https://docs.juce.com/master/group__juce__core-memory.html#gacd79c49d860c4386b12231ca141a41b9

As the example there shows, you need to call deleteInstance on your singleton at shutdown.

greate thanks. now I am using sharedobjectpointer instead of singleton to be more safe bettween windows.

You can inherit your Singleton from DeletedAtShutdown, which will take care of deleting without leaking (like the name suggests).

The difference to SharedResourcePointer is, that the SharedResourcePointer is created on the fly and deleted as soon as possible, vs. the singleton stays alive until the application shuts down or it is deleted manually.

1 Like

that’s ok… I use shareresourcepointer fine now. as I will load global vars in the main,and main will never exist before other window/modules.
thanks