【问题标题】:C++ static classes & shared_ptr memory leaksC++ 静态类和 shared_ptr 内存泄漏
【发布时间】:2011-02-26 06:41:53
【问题描述】:

我不明白为什么下面的代码会产生内存泄漏(我正在使用带有静态类实例的boost::shared_ptr)。有人可以帮我吗?

#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;
}

【问题讨论】:

  • 为什么在参数or-list里面还有第二次调用_CrtSetDbgFlag()
  • 这是一种将两个语句合二为一的方法
  • 我不明白。为什么不“或”四个标志,而是调用“或”列表中的函数?
  • @sharptooth 最后一个flag 不完全是一个标志,用于查询当前标志状态。我接下来要做的意思是(将这三个标志与当前设置结合起来).
  • 我真的很想了解全局变量中静态字段的动机。有一个太多的“静态”不是吗?

标签: c++ memory-leaks shared-ptr


【解决方案1】:

这是内存泄漏。您正在初始化一个名为 myclass_instance 的 myclass 静态实例。您也在初始化“shared_ptr myclass::ptr”。

根据 Stroustrup[3],静态变量按照定义的顺序进行初始化。因此,您有 myclass_instance 的静态定义,它在构造时初始化内部 ptr。但是,您随后有了静态 myclass::ptr 的定义,它调用 shared_ptr 的默认构造函数。

这是一个经典的静态排序问题的例子。编译器认为 myclass::ptr 实际上并没有被初始化,所以没有破坏原来的 shared_ptr。相反,它只是被泄露了。

您将需要某种裸指针。如果您使用的是 C++11,则可以使用三元赋值语句执行 Nifty Counter Technique,如果您确定对象已被初始化,则该语句会移动到自身。这很粗糙,但它确实有效。

这是我在 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;
}

有关详细信息,请参阅以下内容:

  1. Lakos, J, 1996,大型 C++ 软件设计。第 7.8.1.3 节, Addison Wesley,马萨诸塞州雷丁市。
  2. 迈耶斯,S,2005,有效 C++,第三版。第 4 项:确保对象已初始化 在它们被使用之前。 Addison Wesley,马萨诸塞州雷丁市。
  3. Stroustrup,B,2000,C++ 编程语言特别版。 第 10.4.9 节,Addison Wesley,马萨诸塞州雷丁市。

【讨论】:

  • 我认为这是唯一正确的答案,由于 A 和 A::ptr 的顺序不同,anon 的早期答案不是正确的转录。
【解决方案2】:

猜测 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() {
}

打印出来:

ctor
dtor

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2011-10-04
      • 2012-08-08
      • 2013-11-06
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      • 2011-02-13
      • 2020-05-11
      相关资源
      最近更新 更多