[英]Possible memory leak using GetHBITMAP() and MFC CStatic::SetBitmap()
我每100毫秒調用一次以下函數。 目的是從renderBuffer中獲取圖像,調整其大小,然后使用SetBitmap()將其顯示在對話框的CStatic控件中。 問題是執行此功能時,我每秒觀察到相當大的內存使用高峰。 它在CStatis控件中顯示調整大小的圖像沒有問題,但是我在任務管理器中看到,每個第二個進程都會分配額外的4 MB內存,並且永遠不會停止,直到該進程用完內存為止。
這是代碼,如果您有任何想法,請告訴我。
void CAppDlg::UpdatePreview( const RenderBuffer* renderBuffer )
{
HBITMAP hbmReturn = NULL;
Gdiplus::Bitmap *bmPhoto = NULL;
Gdiplus::Bitmap bmPhoto( THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT );
CBitmap Bmp1;
Gdiplus::Bitmap image( 780, 780, 4*780, PixelFormat32bppARGB, renderBuffer->buffer );
int sourceWidth = image.GetWidth();
int sourceHeight = image.GetHeight();
int destX = 0,
destY = 0;
float nPercent = 0;
float nPercentW = ((float)THUMBNAIL_WIDTH/(float)sourceWidth);;
float nPercentH = ((float)THUMBNAIL_HEIGHT/(float)sourceHeight);
if(nPercentH < nPercentW)
{
nPercent = nPercentH;
destX = (int)((THUMBNAIL_WIDTH - (sourceWidth * nPercent))/2);
}
else
{
nPercent = nPercentW;
destY = (int)((THUMBNAIL_HEIGHT - (sourceHeight * nPercent))/2);
}
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
bmPhoto.SetResolution( image.GetHorizontalResolution(), image.GetVerticalResolution() );
Gdiplus::Graphics *grPhoto = Gdiplus::Graphics::FromImage( &bmPhoto );
Gdiplus::Color colorW(255, 255, 255, 255);
grPhoto->Clear( colorW );
grPhoto->SetInterpolationMode( Gdiplus::InterpolationModeHighQualityBicubic );
grPhoto->DrawImage( &image, Gdiplus::Rect(destX, destY, destWidth, destHeight) );
bmPhoto.GetHBITMAP( colorW, &hbmReturn );
m_BitmapPreview.SetBitmap( hbmReturn ); // ---- without this line memory usage doesn't go up rapidly every second.
DeleteObject( hbmReturn ); // ---- returns non-zero which would point out that it was freed properly.
delete grPhoto;
}
謝謝你的幫助!
問候。
我猜你應該使用DeleteObject 。
我認為代碼如下所示:
// ..............
bmPhoto.GetHBITMAP( colorW, &hbmReturn );
HBITMAP prev = m_BitmapPreview.SetBitmap( hbmReturn ); // ---- without this line memory usage doesn't go up rapidly every second.
if (NULL != prev)
{
DeleteObject(prev); // *** do not forget to delete the previously associated bitmap
}
DeleteObject( hbmReturn ); // ---- returns non-zero which would point out that it was freed properly.
// .........
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.