You have two solutions here:
- Don't delete delete it (you're in C++, you use new and delete, right? ;) ). Almost all OSes today will "free" the memory allocated by the application anyway once it's finished. But that's not a good solution, that make memory leaks hard to detect for example.
- Encapsulate your pointer into a class (as member), then use this class as the type of your static. That way, you know the class destructor will be called at the end of the application. You then just delete your data in the destructor and the work is done and clean. That's the power of RAII.
I suggest you do 2, that's a really clean way to do it.
Here is a simple example. Instead of doing this
static Thing* things = new Thing(); // or whatever way to initialize, here or in a specific function
You will do that :
class ThingManager // or whatever name you like
{
public:
ThingManager( Thing* thing ) : m_thing( thing ) { }//or create it here? whatever solution suits your way of creating the data
~ThingManager() { delete m_thing; } // THAT's the important part!
Thing* instance() const { return m_thing; } // or whatever accessor you need, if you need one
private:
Thing* m_thing;
};
and then
static ManagedThing thing; // now i can access it via thing.instance()
When the program ends, the static variable (that is not pointer anymore) will be destroyed and it's destructor will be called to do that.
It's written just to give you an idea of how you can do that.