I would like my class to have a static pointer to a dynamically allocated region of memory. I understand how to initialize it - in my case I will initialize it when the first object needs it. However, I don't know when/where in the code to free it. I'd like to free it when the program terminates.
I might be able to free the pointer in my objects' destructor, but then I'd have to maintain an object count to see if it's safe to free when the object is the last object in use.
Is there a more elegant way to do this?
Please let me know.
Thanks, jbu
You have two solutions here:
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.
Throw it in a smart pointer. It will have static lifetime and be destroyed after main
returns:
static std::auto_ptr<T> thePointer;
Another option is to register your own atexit
function:
// static void YourClass::freePointer(void) { delete getPointer(); } // static T* YourClass::getPointer(void) { if (!thePointer) { thePointer = new T; atexit(freePointer); } return thePointer; }
Which will have the same effect. Another option you already mention is to keep a static counter. Note you can actually wrap that up pretty effectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With