Is this function guaranteed not to create any memory leaks (there is no additional allocation in SomeObject):
void FreeFunction()
{
static boost::scoped_ptr<SomeObject> MyStaticObject(new SomeObject);
}
I never used the combination of static allocation and a smart pointer. It seems to work fine with my compiler, but I would like to know if this always cleans up the allocated memory.
Yes, there is no memory leak.
The
statichere means that the variableMyStaticObjectis initialized on first call toFreeFunction()and then it stays alive throughout the lifetime of the program(just like any other static variable would).The C++ run-time arranges and makes sure that
MyStaticObjectis destroyed at some point.