今天我在做一個從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(),請問是什么原因呢?
uj5u.com熱心網友回復:
rgbabits未釋放記憶體uj5u.com熱心網友回復:
自己申請的記憶體一般自己釋放吧,這里沒自動釋放你申請的記憶體,可能是QImage內部直接用指標了。平常會自己釋放是自己創建記憶體,析構的時候就自己釋放吧。uj5u.com熱心網友回復:
一般遵循“誰申請誰釋放”的原則。uj5u.com熱心網友回復:
這個是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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/253613.html
標籤:C++ 語言
下一篇:左移右移的問題
