【发布时间】:2013-04-28 14:35:56
【问题描述】:
我已经使用指针创建了一个数组,然后在析构函数中写了一个删除过程
class cBuffer{
private:
struct entry {
uint64_t key;
uint64_t pc;
};
entry *en;
public:
cBuffer(int a, int b, int mode)
{
limit = a;
dist = b;
md = mode;
en = new entry[ limit ];
for (int i=0; i<limit; i++) {
en[i].key = 0;
en[i].pc = 0;
}
};
~cBuffer() { delete [] en; }
...
}
在另一个类中,我像这样使用 cBuffer:
class foo() {
cBuffer *buf;
foo()
{
buf = new cBuffer(gSize, oDist, Mode);
}
};
然而,valgrind 抱怨新的操作符
==20381== 16,906,240 bytes in 32 blocks are possibly lost in loss record 11,217 of 11,221
==20381== at 0x4A0674C: operator new[](unsigned long) (vg_replace_malloc.c:305)
==20381== by 0x166D92F8: cBuffer::cBuffer(int, int, int)
【问题讨论】:
-
你没有遵守三法则。
-
但这不太可能是内存泄漏的原因;您如何创建和销毁
cBuffer对象?我最好的猜测是你用new cBuffer泄漏了整个对象,而没有对应的delete。 -
您使用的是裸指针。你不应该。
-
@mahmood:我希望 valgrind 报告这两个漏洞。你是说它只报告那个?
标签: c++ memory-leaks valgrind