嵌套的QVector指针内存处理 [英] Nested QVector pointer memory handling

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

问题描述

我继承了一个实质性的Qt5项目,在该项目中,累积内存泄漏已成为一个严重的问题. (是的,应该很少容忍内存泄漏,但是要考虑到现实生活中的预算和时间限制...).

I've inherited a substantial Qt5 project, where the accumulative memory leakage is becoming a serious issue. (Yes, memory leakage should rarely be tolerated but with real life budget and time constraints...).

此GUI将图像数据读取到体素类的对象中,以图形方式显示.数据来自文件或缓冲区(如果实时获取),并存储为嵌套qvector,即:

This GUI reads image data into objects of a voxel class, to be displayed graphically. The data comes either from file or buffer (if acquired live) and is stored as a nest qvector, ie:

QVector < QVector <Voxel *> > cVoxel;

从文件中读取图像时,使用QVector.resize(0)初始化cVoxel.

When an image is read from file, cVoxel is initialized using QVector.resize(0).

cVoxel.resize(0);

打开保存到文件的图像时,将创建一个本地Voxel指针并将其推到cVoxel的末尾,每个像素一次,因此遍历所有行和列:

When an image save to file is opened, a local Voxel pointer is created and pushed to the end of cVoxel, once for every pixel so looping over all rows and columns:

for (iRow = 0; iRow < nRows; ++iRow)
{
   for (iCol = 0; iCol < nCols; ++iCol)
   {
      Voxel *v = new Voxel;
      cVoxel[iRow].push_back(v);
      // Code for reading data into cVoxel removed here
      ...
   }
}

由以下有用的注释提供,通过在我的CTOR中嵌套破坏cVoxel QVector,我现在在Windows任务管理器中看到内存使用率减少方面取得了一些成功.遵循以下原则:

Courtesy of the useful comments below, I've now had some success in seeing memory usage decrease in the Windows Task Manager, by nesting destruction of the cVoxel QVector in my CTOR. Along the lines of:

for (iRow = 0; iRow < nRows; iRow++)
{
    for (iCol = 0; iCol < nCols; iCol++)
    {
        delete cVoxel[iRow][iCol];
    }
}

理想情况下,进行重大重写是最好的解决方案.但是在现实世界中,我将不得不尝试解决更大的泄漏,并希望在没有足够的资源可用于更理想的解决方案之前就足够了.

Ideally, a major rewrite is the best solution. But in the real world, I'll have to try and fix the bigger leaks and hope that's enough until there's enough resources available for a more ideal solution.

  • 我已经看过Voxel本身的内存泄漏,但是那里没有明显的东西.
  • 我的研究表明,查看Windows Task Manager的内存消耗并不完全可靠(Win7并非实时OS ..),但是如果打开文件,则应用程序内存消耗将从16M增加到81.5M ,那么如果成功释放了cVoxel中分配的内存,那么肯定应该有一些的内存减少吗?如果我继续打开和关闭图像,则应用程序的内存消耗将以类似的方式不断增加.关闭任何/所有打开的图像后,它都不会减少.
  • 目前,没有尝试释放(使用new运算符)分配给cVoxel的任何内存.我尝试了几种方法(并阅读以了解更多信息),但到目前为止运气不佳.
  • QVector非常擅长处理自己的内存,但是我坚持使用此嵌套QVector设置,仅依靠QVector的squeeze(),resize()或类似方法只会泄漏内存(已经是项目中其他变量的情况..我已经通过Visual Leak Detector运行了该项目,所以我有了一个想法,哪些是严重的罪魁祸首,哪些是小鱼)
  • I've looked at memory leakages in Voxel itself, but there's nothing obvious there.
  • My research reveals that looking at the Windows Task Manager for memory consumption isn't entirely reliable (Win7 isn't a Real-Time OS..), but if opening a file increases the application memory consumption from 16M to 81.5M, then surely there should be some memory decrease if the allocated memory in cVoxel is successfully released? If I keep opening and shut images, the app's memory consumption keeps increasing in similar step. It never decreases after closing any/all opened images.
  • Right now, there's no attempt to release any memory assigned (using the new operator) to cVoxel. I have tried a few approaches (and read to learn more), but so far litte luck.
  • QVector is excellent at taking care of it's own memory handling, but I'm stuck with this nest QVector setup, and simply relying on QVector's squeeze(), resize(), or similar will only leak memory (which is already the case for other variables in the project.. I have run the project through Visual Leak Detector, so I've an idea which are the serious culprits, and which ones are small fish)

----编辑----

下面为混乱的ad-hod评论道歉,但这肯定可以帮助我减少内存泄漏(希望在适当的时候完全停止.).

Apologies for the messy ad-hod commenting below, but this is certainly helping me reduce the memory leakages (complete stoppage will hopefully happen in due course..).

我已经在上面进行了内联编辑,以(希望)使此帖子更清晰,并删除了我的最佳案例,因为它对内存泄漏没有影响.上面的重大更改是(2)斜体简短的段落.

I've edited in-line above to (hopefully) make this post clearer, and removed my best case effort as it had no impact on the memory leak. The significant alteration above are the (2) brief paragraphs in italics.

我还需要研究与@richardcitter(sp?)多态性有关的建议.

I also need to investigate @richardcitter (sp?) polymorphism related suggestion.

--- EDIT3 ---

删除了Edit2,单独发布了(新)问题

Removed Edit2, posted that (new) question separately here.

此外,我非常有信心下面的答案可以解决这个问题-我只需要弄清楚如何使用qvector.resize()或找到解决方法.

Also, I'm pretty confident the answer below should fix this question - I just need to figure out how to use qvector.resize() or find a workaround to it.

推荐答案

@SomeProgrammerDude:您给我9/10的解决方案.我不知道应该编辑您的答案还是使用此答案,因此主持人请相应地进行编辑.

@SomeProgrammerDude: You got me 9/10 towards the solution. I don't know whether I should edit your answer or use this, so moderator(s) please edit accordingly.

所概述因此,,我最终决定不使用智能指针.上面的解决方案(对我而言)引入了编译器试图引用已删除函数的问题.否则,请进行以下修改是正确的:

As outlined in a connected SO post, I decided against smart pointers in the end. The above solution introduced (for me) the issue of the compiler trying to reference a deleted function. Otherwise it is correct with a couple of modifications:

QVector < QVector <Voxel *> > cVoxel;

初始化:

cVoxel.resize(0);

内存分配:

{
   for (int i = 0; i < rows; ++i)
   {
      cVoxel.push_back( QVector <Voxel *> () );
      for (int j = 0 ; j < cols ; ++j)
      {
         Voxel *v = new Voxel;
         cVoxel[i].push_back(v);
      }
   }
}

最后DTOR释放内存:

Finally DTOR releasing the memory:

   int iRow, iCol;
   for (iRow = 0; iRow < rows; iRow++)
   {
      for (iCol = 0; iCol < cols; iCol++)
      {
           delete cVoxel[iRow][iCol];
      }
   }

这篇关于嵌套的QVector指针内存处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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