我们在课堂上使用 pimpl 成语。pimpl 结构在包含 pimpl 指针的类中声明,如下所示:
struct MyClassImpl;
friend struct MyClassImpl;
boost::scoped_ptr<MyClassImpl> m_Impl;
pimpl 的实现在一个名为 MyClassImpl.cpp 的单独文件中,例如:
struct MyClass::MyClassImpl
{
QString m_Name;
int m_Type;
double m_Frequency;
int m_DefaultSize;
QVariant m_DefaultValue;
boost::shared_ptr<SomeOtherClass> m_SomeOtherClass;
~MyClassImpl()
{
}
};
在包含 pimpl 指针的类的构造函数中,我将在成员变量初始化列表中具有类似
m_Impl(new MyClassImpl())
现在,我们在源代码中启用了内存泄漏检测,如下所示:
// Memory leaks detection in Visual Studio
#if defined (_WIN32) && defined (_DEBUG)
# define _CRTDBG_MAP_ALLOC
# include <crtdbg.h>
# define new new(_NORMAL_BLOCK ,__FILE__, __LINE__)
#endif
我发现当程序退出时,MyClassImpl() struct m_Impl(new MyClassImpl()) 会报告内存泄漏:
..\..\src\MyClass.cpp(29) : {290222} normal block at 0x0B9664E0, 48 bytes long.
Data: <X l V Y@> 58 1C 6C 03 56 00 00 00 00 00 00 00 00 00 59 40
我不明白为什么因为 m_Impl 是 boost::scoped_ptr 并且 QString、QVariant 和 shared_ptr 都是托管的。有任何想法吗?