QT 列表追加中报告 Valgrind 内存泄漏
我正在 QT C++ 中使用序列化器。看起来没问题,但 valgrind(memcheck 工具)报告此函数存在内存泄漏。
Valgrind cmd: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 问题吗?
I am using a serializer in QT C++. It looks ok but valgrind (memcheck tool) is reporting a memory leak on this function.
Valgrind cmd: 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 reports that this instance is not freed but it is been used in the QList.
AppNodeRecord *tmp=new AppNodeRecord;
Valgrind output:
==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)
Could it be a valgrind issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QList
不负责释放附加到它的AppNodeRecord
指针,您必须手动执行此操作(qDeleteAll
在这种情况下可能会有所帮助)。但像往常一样,由于缺乏充分的理由,请使用 QList首先避免这种麻烦。
The
QList
isn't responsible for deallocating theAppNodeRecord
pointers you append to it, you have to do it manually (qDeleteAll
might help in that case).But as usual, for lack of a good reason, use
QList<AppNodeRecord>
to avoid this hassle in the first place.Valgrind memcheck 只会告诉您存在内存泄漏。如果像您的情况一样,有一个,它会报告发生内存分配的函数(
new
语句)。要消除这种泄漏,您必须删除所有已动态分配的元素。在您的情况下,正如 Idan K 所写,您可以在类的析构函数中使用通用 Qt 算法 qDeleteAll(objAppNodeListRecord) ,或者您可以使用更明确的版本,如下所示:
Valgrind memcheck only tells you that there is a memory leak. If, as in your case, there is one, it reports the function where the memory allocation happened (the
new
statement).To get rid of this leak, you have to delete all the elements that have been dynamically allocated. In your case, as Idan K wrote, you can use the generic Qt algorithm
qDeleteAll(objAppNodeListRecord)
for instance in the destructor of your class or you can use a more explicit version as follow: