【发布时间】:2015-10-04 12:09:52
【问题描述】:
我正在尝试了解现代 C++ 中智能指针的使用,并且我编写了一个小而简单的程序来测试 valgrind。问题是下面的例子:
#include <iostream>
#include <memory>
class Base {
private:
virtual double meth_1( double x ) const = 0;
virtual void meth_2( int y ) const = 0;
protected:
Base()
{
std::cout << "ctor of base for: " << this << std::endl;
}
public:
virtual ~Base()
{
std::cout << "dtor of base for: " << this << std::endl;
}
double IMeth_1( double x ) const
{
return meth_1(x);
}
void IMeth_2( int y ) const
{
meth_2(y);
}
};
class Derived_1 : public Base {
private:
double meth_1( double x ) const final
{
return x + 5.0;
}
void meth_2( int y ) const final
{
std::cout << (y + 5) << std::endl;
}
public:
Derived_1() : Base()
{
std::cout << "ctor of Derived_1: " << this << std::endl;
}
~Derived_1()
{
std::cout << "dtor of Derived_1: " << this << std::endl;
}
};
class Derived_2 : public Base {
private:
double meth_1( double x ) const final
{
return x + 10.0;
}
void meth_2( int y ) const final
{
std::cout << (y + 10) << std::endl;
}
public:
Derived_2() : Base()
{
std::cout << "ctor of Derived_2: " << this << std::endl;
}
~Derived_2()
{
std::cout << "dtor of Derived_2: " << this << std::endl;
}
};
void Fun( const Base& crBase )
{
crBase.IMeth_2( 5 );
}
int main( int argc, char* argv[] ) {
std::unique_ptr< Base > upBase;
for ( std::size_t idx = 0ul; idx < 2ul; idx++ ) {
upBase = std::make_unique< Derived_1 >();
std::cout << upBase->IMeth_1( idx ) << std::endl;
upBase->IMeth_2( idx );
std::cout << "----------" << std::endl;
}
for ( std::size_t idx = 0ul; idx < 2ul; idx++ ) {
upBase = std::make_unique< Derived_2 >();
std::cout << upBase->IMeth_1( idx ) << std::endl;
upBase->IMeth_2( idx );
std::cout << "----------" << std::endl;
}
upBase = std::make_unique< Derived_1 >();
Fun( *upBase );
return 0;
}
使用valgrind --leak-check=full --show-leak-kinds=all <prog_name> 运行时会出现内存泄漏:
==32350== HEAP SUMMARY:
==32350== in use at exit: 72,704 bytes in 1 blocks
==32350== total heap usage: 6 allocs, 5 frees, 72,744 bytes allocated
==32350==
==32350== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==32350== at 0x4C28C10: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==32350== by 0x4EBE1EF: pool (eh_alloc.cc:117)
==32350== by 0x4EBE1EF: __static_initialization_and_destruction_0 (eh_alloc.cc:244)
==32350== by 0x4EBE1EF: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:307)
==32350== by 0x400F279: call_init.part.0 (in /usr/lib/ld-2.22.so)
==32350== by 0x400F38A: _dl_init (in /usr/lib/ld-2.22.so)
==32350== by 0x4000DB9: ??? (in /usr/lib/ld-2.22.so)
==32350==
==32350== LEAK SUMMARY:
==32350== definitely lost: 0 bytes in 0 blocks
==32350== indirectly lost: 0 bytes in 0 blocks
==32350== possibly lost: 0 bytes in 0 blocks
==32350== still reachable: 72,704 bytes in 1 blocks
==32350== suppressed: 0 bytes in 0 blocks
==32350==
==32350== For counts of detected and suppressed errors, rerun with: -v
==32350== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
72,704 字节的块在退出时是否仍在使用中是误报,还是我误用了智能指针?我假设我没有进行任何类型的切片,因为每次删除对象时都会调用基本 dtor。
对不起,如果这是一个愚蠢的问题,但我在 SO 中找不到任何与 valgrind/false positive/unique_ptr 相关的主题。此外,我不知道可能在unique_ptr 中创建了类似于shared_ptr 中的任何其他块来跟踪对象。
编辑: 不是 Still Reachable Leak detected by Valgrind 的重复项,因为在我的情况下我没有使用线程(众所周知,valgrind 在尤其是 OpenMPI 环境中会给出误报)。此外,在另一个问题中,问题是通过对提供的代码进行适当修改来解决的。尽管如此,对于什么叫真正的内存泄漏,没有任何共识——也就是说,在退出时仍在使用的可达块是否应该 /em> 是否被视为内存泄漏。
【问题讨论】:
-
看起来像一个 valgrind 错误,它正在检测自己的分配...您对
std::unique_ptr的使用看起来不错。 -
看一眼代码,它看起来不错,但您应该尝试减少它,因为这里似乎仍然有很多不必要的代码来重现问题。作为在 valgrind 中测试小东西的一般规则,将整个测试放入一个函数并多次调用它。然后,如果丢失块的数量增加,你就知道你有真正的内存泄漏。
-
感谢你们两位的cmets。我将尝试这种函数方法来查看退出时使用的块是增加还是保持不变,@Dave。
-
您确定这是内存泄漏吗? Valgrind 似乎抱怨可引用(可访问,非泄漏)分配的数据,这些数据可能由系统库分配一次,并打算在整个应用程序生命周期中保留。
标签: c++ valgrind c++14 smart-pointers