I can't understand why does the following code produce memory leaks (I am using boost::shared_ptr
with static class instance). Could someone help me?
#include <crtdbg.h>
#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
#define _CRTDBG_MAP_ALLOC
#define NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
static struct myclass {
static shared_ptr<int> ptr;
myclass() {
ptr = shared_ptr<int>(NEW int);
}
} myclass_instance;
shared_ptr<int> myclass::ptr;
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF |
_CRTDBG_CHECK_ALWAYS_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
return 0;
}
At a guess the CRT is reporting a false positive - the following code illustrates that the shared pointer is working correctly, at least with g++
It prints:
Most likely the leak is detected before the global objects get destroyed and
shared_ptr
has a chance to release the object, so it's likely a false leak.This is a memory leak. You are initializing a static instance of myclass called myclass_instance. You are also initializing the "shared_ptr myclass::ptr".
According to Stroustrup[3], statics are initialized in the order that they are defined. Therefore you have the static definition of myclass_instance, which initializes the internal ptr on construction. However, you then have the definition of the static myclass::ptr, which invokes the default constructor for shared_ptr.
This is an example of the classic statics ordering problem. The compiler thinks that myclass::ptr wasn't actually initialized, so there's no destruction of the original shared_ptr. Instead, it is just leaked.
You'll need a bare pointer of some kind. If you're using C++11, you can do the Nifty Counter Technique with a ternary assignment statement which does a move to itself if you determine that the object has already been initialized. It's pretty rough, but it works.
Here's how I'd do it in C++11:
See the following for more information: