我在 QT C++ 中使用序列化程序。看起来不错,但 valgrind(memcheck 工具)报告此函数存在内存泄漏。
瓦尔格林命令:valgrind --tool=memcheck --leak-check=full
QDataStream &operator>>( QDataStream &in, QList<AppNodeRecord *> *objAppNodeListRecord)
{
quint32 len;
in >> len;
objAppNodeListRecord->clear();
for(quint32 i = 0; i < len; ++i)
{
AppNodeRecord *tmp=new AppNodeRecord;
in >> tmp;
objAppNodeListRecord->append(tmp);
if (in.atEnd())
break;
}
return in;
}
Valgrind 报告说这个实例没有被释放,但它已在 QList 中使用。
AppNodeRecord *tmp=new AppNodeRecord;
Valgrind 输出:
==19503== 1,445 (68 direct, 1,377 indirect) bytes in 1 blocks are definitely lost in loss record 1,540 of 1,568
==19503== at 0x4026351: operator new(unsigned int) (vg_replace_malloc.c:255)
==19503== by 0x8058562: operator>>(QDataStream&, QList<AppNodeRecord*>*) (zbDbs_NodeMgmt.cpp:206)
==19503== by 0x804D53C: main (main.cpp:53)
这可能是一个valgrind问题吗?