简体   繁体   English

C ++静态类和shared_ptr内存泄漏

[英]C++ static classes & shared_ptr memory leaks

I can't understand why does the following code produce memory leaks (I am using boost::shared_ptr with static class instance). 我不明白为什么以下代码会产生内存泄漏(我在静态类实例中使用boost::shared_ptr )。 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;
}

This is a memory leak. 这是内存泄漏。 You are initializing a static instance of myclass called myclass_instance. 您正在初始化myclass的静态实例myclass_instance。 You are also initializing the "shared_ptr myclass::ptr". 您还正在初始化“ shared_ptr myclass :: ptr”。

According to Stroustrup[3], statics are initialized in the order that they are defined. 根据Stroustrup [3],按定义的顺序初始化静态变量。 Therefore you have the static definition of myclass_instance, which initializes the internal ptr on construction. 因此,您具有myclass_instance的静态定义,该定义初始化构造时的内部ptr。 However, you then have the definition of the static myclass::ptr, which invokes the default constructor for shared_ptr. 但是,您然后有了静态myclass :: ptr的定义,该定义调用了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. 编译器认为myclass :: ptr实际上并未初始化,因此不会破坏原始的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. 如果您使用的是C ++ 11,则可以使用三元赋值语句执行Nifty Counter Technique,如果您确定该对象已经初始化,则该语句会自动执行。 It's pretty rough, but it works. 这很粗糙,但是可以。

Here's how I'd do it in C++11: 这是我在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: 有关更多信息,请参见以下内容:

  1. Lakos, J, 1996, Large-Scale C++ Software Design. Lakos,J,1996,大规模C ++软件设计。 Section 7.8.1.3, Addison Wesley, Reading, Massachusetts. 马萨诸塞州雷丁市Addison Wesley第7.8.1.3节。
  2. Meyers, S, 2005, Effective C++, Third Edition. Meyers,S,2005,有效C ++,第三版。 Item 4: Make sure that objects are initialized before they're used. 第4项:确保在使用对象之前将其初始化。 Addison Wesley, Reading, Massachusetts. Addison Wesley,马萨诸塞州雷丁。
  3. Stroustrup, B, 2000, The C++ Programming Language Special Edition. Stroustrup,B,2000年,《 C ++编程语言特别版》。 Section 10.4.9, Addison Wesley, Reading, Massachusetts. 第10.4.9节,马萨诸塞州雷丁的Addison Wesley。

最有可能在全局对象被销毁之前检测到泄漏,并且shared_ptr有机会释放该对象,因此很可能是错误的泄漏。

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++ 猜测CRT报告的是误报-以下代码说明了共享指针至少在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM