QT 列表追加中报告 Valgrind 内存泄漏

发布于 2024-12-07 03:20:42 字数 1125 浏览 0 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坦然微笑 2024-12-14 03:20:42

QList 不负责释放附加到它的 AppNodeRecord 指针,您必须手动执行此操作(qDeleteAll 在这种情况下可能会有所帮助)。

但像往常一样,由于缺乏充分的理由,请使用 QList首先避免这种麻烦。

The QList isn't responsible for deallocating the AppNodeRecord 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.

没有伤那来痛 2024-12-14 03:20:42

Valgrind memcheck 只会告诉您存在内存泄漏。如果像您的情况一样,有一个,它会报告发生内存分配的函数(new 语句)。

要消除这种泄漏,您必须删除所有已动态分配的元素。在您的情况下,正如 Idan K 所写,您可以在类的析构函数中使用通用 Qt 算法 qDeleteAll(objAppNodeListRecord) ,或者您可以使用更明确的版本,如下所示:

foreach (AppNodeRecord *element, objAppNodeListRecord)
{
  delete element;
}

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:

foreach (AppNodeRecord *element, objAppNodeListRecord)
{
  delete element;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文