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 开发者_JS百科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;
}
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:
#include <crtdbg.h>
#include <memory>
using std;
#define _CRTDBG_MAP_ALLOC
#define NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
// Note that the count could also be a field in an initializer static used in the Nifty Counter
// Technique covered in many texts.
static int count = 0; // This gets implicitly initialized to 0 by the executable load into memory.
static struct myclass {
static shared_ptr<int> ptr;
myclass() {
if (count++ == 0) {
ptr = make_shared<int>(0); //initialization
}
}
} myclass_instance;
shared_ptr<int> myclass::ptr = count == 0 ? make_shared<int>(0) : move(myclass::ptr);
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF |
_CRTDBG_CHECK_ALWAYS_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
return 0;
}
See the following for more information:
- Lakos, J, 1996, Large-Scale C++ Software Design. Section 7.8.1.3, Addison Wesley, Reading, Massachusetts.
- Meyers, S, 2005, Effective C++, Third Edition. Item 4: Make sure that objects are initialized before they're used. Addison Wesley, Reading, Massachusetts.
- Stroustrup, B, 2000, The C++ Programming Language Special Edition. Section 10.4.9, Addison Wesley, Reading, Massachusetts.
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++
#include <iostream>
#include "boost/shared_ptr.hpp"
using namespace std;
using namespace boost;
struct R {
R() {
cerr << "ctor" << endl;
}
~R() {
cerr << "dtor" << endl;
}
};
struct A {
static shared_ptr<R> ptr;
A() {
ptr = shared_ptr<R>(new R);
}
};
shared_ptr<R> A::ptr;
static A a;
int main() {
}
It prints:
ctor
dtor
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.
精彩评论