遍历qvector,嵌套QVector指针内存处理

遍历qvector,嵌套QVector指针内存处理I veinheriteda wheretheaccu Yes memoryleakag

遍历qvector,嵌套QVector指针内存处理

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…).

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 > cVoxel;

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

cVoxel.resize(0);

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

}

}

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.

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)

—-EDIT —-

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..).

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.

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

— EDIT3 —

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

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: 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 > cVoxel;

Initialisation:

cVoxel.resize(0);

Memory assignment:

{

for (int i = 0; i < rows; ++i)

{

cVoxel.push_back( QVector () );

for (int j = 0 ; j < cols ; ++j)

{

Voxel *v = new Voxel;

cVoxel[i].push_back(v);

}

}

}

Finally DTOR releasing the memory:

int iRow, iCol;

for (iRow = 0; iRow < rows; iRow++)

{

for (iCol = 0; iCol < cols; iCol++)

{

delete cVoxel[iRow][iCol];

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/224045.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午12:49
下一篇 2026年3月17日 下午12:49


相关推荐

  • Centos 7 图形化界面安装

    Centos 7 图形化界面安装1.centos安装完成centos72.测试网络是否能进行链接重启网络。[root@localhost~]#servicenetworkrestart出现以下页面,则网络重启成功。测试链接。[root@localhost~]#pingwww.baidu.com出现以下页面则可以链接网络,ctrl+c退出,进入步骤4。如果链接失败进入步骤3。3.进行网络配置首先进入以下文件夹[root@localhost~]#cd/etc/sysconfig/netwo

    2022年5月30日
    65
  • Web安全 支付逻辑漏洞(不用钱 买任何东西.(也能让商家倒贴钱给我们.))

    Web安全 支付逻辑漏洞(不用钱 买任何东西.(也能让商家倒贴钱给我们.))?我们购买东西正常情况下都是要钱的,如果说平台出现了(支付逻辑漏洞.)那么我们就可以,利用这个漏洞实现不用钱买任何东西,???或者是商家倒贴钱给我们来购买他的东西.(这不学起来吗?)

    2022年6月2日
    30
  • kfold交叉验证_SPSS交叉验证法

    kfold交叉验证_SPSS交叉验证法一、前言在机器学习建模过程中,通行的做法是将数据分为训练集和测试集。测试集是与训练独立的数据,完全不参与训练,用于最终模型的评估。在训练过程中,经常会出现过拟合的问题,就是模型可以很好的匹配训练数据,却不能很好在预测训练集外的数据。如果此时就使用测试数据来调整模型参数,就相当于在训练时已知部分测试数据的信息,会影响最终评估结果的准确性。通常的做法是在训练数据再中分出一部分做为验证(Validation)数据,用来评估模型的训练效果。验证数据取自训练数据,但不参与训练,这样可以相对客观的评估模型对于训

    2026年1月31日
    4
  • js回调函数的使用

    js回调函数的使用在第一次学 js 的时候 很多人估计对回调函数很蒙 今天有空梳理总结下回调函数 算是一次学习 算是一次总结 也算是一次分享 回调函数执行流程说明 fun 是被调用的函数 在调用 fun 函数时传递一个被回调的函数 callback 1 2 3 4 为执行步骤 箭头指向为调用 执行流程 1 在代码中 fun 首先被执行 2 fun 函数执行 js 的执行转移到 fun 函数内部 fun 函数内部的语句从上往下执行 3 在 fun 函数中调用 callback4 在调用 fun 函数时传入的函数 callback 被执行 5 执行 ca

    2026年3月18日
    2
  • 最快的方法来清除Chrome浏览器DNS高速缓存

    最快的方法来清除Chrome浏览器DNS高速缓存

    2022年1月12日
    81
  • shell数组变量赋值_形参可以是常量变量或表达式

    shell数组变量赋值_形参可以是常量变量或表达式1.定义数组bash支持一维数组(不支持多维数组),并且没有限定数组的大小。类似于C语言,数组元素的下标由0开始编号。获取数组中的元素要利用下标,下标可以是整数或算术表达式,其值应大于或等于0。在Shell中,用括号来表示数组,数组元素用”空格”符号分割开。定义数组的一般形式为:【示例】定义数组:array_name=(value0value1value2value3)数组的值类型任意,个数不限可以不使用连续的下标,而且下标的范围没有限制:array_name=([0]

    2025年6月26日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号