QProcess导致内存泄漏 [英] QProcess causes memory leak

查看:971
本文介绍了QProcess导致内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个C ++ / Qt5.1应用程序,它使用QProcess启动另一个程序,然后等待结果。每次我运行这个代码,valgrind说内存丢失在第2行(开始行)。

  QProcess命令; 
command.start(commandpath,myParameters);
if(command.waitForStarted(waitToStart)){
command.write(myStdIn.toLatin1());
command.closeWriteChannel()?
if(command.waitForFinished(waitToFinish)){
myStdOut = command.readAllStandardOutput();
myStdErr = command.readAllStandardError();
}
}
command.deleteLater();

我添加了deletelater()行,但它没有帮助。 (注意,如果'commandpath'程序没有成功运行,例如,当我尝试运行一个不存在的程序时,内存丢失才会发生。)



有人可以解释为什么,以及如何解决这种内存丢失?



这里有一些valgrind输出如果有帮助:



<$在RunProcessWorker :: run(RunProcessWorker :: EMutex,QString,QString,QString,bool,QString,QStringList)中的1个块中的16个字节肯定丢失了678
,QStringList,QString,QString& amp; amp; amp; amp; amp; unsigned int,unsigned int,unsigned long long& amp; RunProcessWorker :: EResultCodes& amp; QProcess :: ProcessError& amp; amp; amp; amp; amp; amp; amp /mnt/lserver2/data/development/haast/src/systemcommands/runprocessworker.cpp:249
1:operator new [](unsigned long)in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
2:/opt/Qt5.1.0/5.1.0/gcc_64/lib/libQt5Core.so.5.1.0
3:QProcess :: open(QFlags& lt; QIODevice :: OpenModeFlag& gt;) in /opt/Qt5.1.0/5.1.0/gcc_64/lib/libQt5Core.so.5.1.0
4:QProcess :: start(QString const& amp; QStringList const& amp; QFlags& lt; QIODevice :: OpenModeFlag& gt;)in /opt/Qt5.1.0/5.1.0/gcc_64/lib/libQt5Core.so.5.1.0
5:RunProcessWorker :: run(RunProcessWorker :: EMutex,QString,QString ,QString,bool,QString,QStringList,QStringList,QString,QString& amp; amp; amp; amp; amp; unsigned int,unsigned int,unsigned long long& amp; RunProcessWorker :: EResultCodes& amp; QProcess :: ProcessError& amp ;,int& amp;)in< a href =file:///mnt/lserver2/data/development/haast/bin/debug/../../src/systemcommands/runprocessworker.cpp:249> ; /mnt/lserver2/data/development/haast/src/systemcommands/runprocessworker.cpp:249< / a>


解决方案

并非 valgrind >是真正的内存泄漏,或者你应该关心的泄漏。只要内存泄漏来自一个库,并且不会增长,即使你做了失败的事情很多次,那么不要担心它。



偶尽管在应用程序中它被认为是不好的做法,但是库可能从堆分配任何东西,而这些东西从未被释放。库可以添加一个退出处理程序来释放这些,但它会减慢程序的退出,没有真正的收益,对于操作系统在一个大块发布的资源。



因此, valgrind 支持抑制错误。使用Qt执行此操作的最简单方法是在Qt Creator下运行valgrind






如果你担心这实际上是一个Qt错误,那么你应该编写代码,在漏洞操作中循环一百万次。如果泄漏的大小增加,那么它是坏的,你应该可以使用代码提交错误报告再现它。即使它是一个一次性的泄漏在不寻常的代码路径,它有固定的可能是好的,而不是留下无用的分配杂乱的堆。


I'm building a C++/Qt5.1 app which uses QProcess to launch another program, then wait for the result. Every time I run this code, valgrind says memory is lost on line 2 (the start line).

QProcess command(this);
command.start(commandpath, myParameters);
if (command.waitForStarted(waitToStart)) {
    command.write(myStdIn.toLatin1());
    command.closeWriteChannel();
    if (command.waitForFinished(waitToFinish)) {
        myStdOut = command.readAllStandardOutput();
        myStdErr = command.readAllStandardError();
    }
}
command.deleteLater();

I added the deletelater() line but it doesn't help. (Note that the memory loss only occurs if the 'commandpath' program doesn't run successfully - for example, when I try to run a non-existant program).

Can someone explain why, and how to resolve this memory loss?

Here's some valgrind output if that helps:

16 bytes in 1 blocks are definitely lost in loss record 57 of 678
  in RunProcessWorker::run(RunProcessWorker::EMutex, QString, QString, QString, bool, QString, QStringList, QStringList, QString, QString&amp;, QString&amp;, unsigned int, unsigned int, unsigned long long&amp;, RunProcessWorker::EResultCodes&amp;, QProcess::ProcessError&amp;, int&amp;) in /mnt/lserver2/data/development/haast/src/systemcommands/runprocessworker.cpp:249
  1: operator new[](unsigned long) in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so
  2: /opt/Qt5.1.0/5.1.0/gcc_64/lib/libQt5Core.so.5.1.0
  3: QProcess::open(QFlags&lt;QIODevice::OpenModeFlag&gt;) in /opt/Qt5.1.0/5.1.0/gcc_64/lib/libQt5Core.so.5.1.0
  4: QProcess::start(QString const&amp;, QStringList const&amp;, QFlags&lt;QIODevice::OpenModeFlag&gt;) in /opt/Qt5.1.0/5.1.0/gcc_64/lib/libQt5Core.so.5.1.0
  5: RunProcessWorker::run(RunProcessWorker::EMutex, QString, QString, QString, bool, QString, QStringList, QStringList, QString, QString&amp;, QString&amp;, unsigned int, unsigned int, unsigned long long&amp;, RunProcessWorker::EResultCodes&amp;, QProcess::ProcessError&amp;, int&amp;) in <a href="file:///mnt/lserver2/data/development/haast/bin/debug/../../src/systemcommands/runprocessworker.cpp:249" >/mnt/lserver2/data/development/haast/src/systemcommands/runprocessworker.cpp:249</a>

解决方案

Not all findings by valgrind are "real" memory leaks, or leaks you should care about. As long as memory "leak" comes from a library, and does not grow even if you do the failing thing many times, then don't worry about it.

Even though it is considered bad practice in applications, libraries may allocate things from heap, which are never freed. The library could add an exit handler to release those, but it would slow down exit of program for no real gain, for resources which are released by the OS anyway in one big chunk.

For this reason, valgrind supports suppressing errors. Easiest way to do this with Qt is to run valgrind under Qt Creator, which has a correct suppression file for Qt libraries by default.


If you are worried that this is actually a Qt bug, then you should write code which does the leaky operation in a loop a million times. If size of leak increases, then it's bad, and you should probably file a bug report with the code to reproduce it. Even if it is a one-time leak in uncommon code path, having it fixed would probably be good, instead of leaving useless allocation to clutter the heap.

这篇关于QProcess导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆