Qt问题,QImage没有自动释放内存

「已注销」 2021-01-28 04:44:46
今天我在做一个从YUYV格式转换为QImage图像转换的实现的时候,在转换线程中进行运算,然后创建QImage对象,通过信号发送到显示的窗口显示,但是发现QImage更新以后,内存爆炸了,QImage并没有把我申请的图像的内存给释放掉。没有搞清楚原因。下面是我做处理的大致代码

QImage yuyvToQImage(int w, int h, uchar* data)
{
int yuyvBitSize = w*h*2;
int *yuyvBits = new int[yuyvBitSize];
memcpy(yuyvBits,data,yuyvBitSize);

int rgbaBitSize = w*h; //一个int保存一个像素
int *rgbaBits = new int[rgbaBitSize];

// 转换代码
delete[] yuyvBits;
delete[] data;

QImage image = QImage((uchar*)rgbaBits,w,h,QImage::Format_ARGB32);
return image;
}


按道理QImage应该会自动释放掉我申请的rgbaBits内存,但是我发现并没有,只能我在更新的时候手动先调用delete[] image.bits(),请问是什么原因呢?
...全文
4798 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2021-01-29
  • 打赏
  • 举报
回复
引用 4 楼 github_37127447的回复:
这个是QT 关于你用的这个构造函数的描述,这个buffer 你要在QImage生命周期结束后自己释放。你可以直接指定一个清理函数.Qimage 就会自己帮你清理了 The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
谢谢谢谢,我懂了,我还是要多看文档啊
熊猫呀 2021-01-28
  • 打赏
  • 举报
回复
这个是QT 关于你用的这个构造函数的描述,这个buffer 你要在QImage生命周期结束后自己释放。你可以直接指定一个清理函数.Qimage 就会自己帮你清理了 The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
赵4老师 2021-01-28
  • 打赏
  • 举报
回复
一般遵循“谁申请谁释放”的原则。
  • 打赏
  • 举报
回复
自己申请的内存一般自己释放吧,这里没自动释放你申请的内存,可能是QImage内部直接用指针了。平常会自己释放是自己创建内存,析构的时候就自己释放吧。
maguangzhi 2021-01-28
  • 打赏
  • 举报
回复
rgbabits未释放内存

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧