如何处理GDI资源泄漏

时间:2011-11-07 13:01:56

标签: visual-c++ gdi

在我的应用程序中,我使用GetDC获得DC,我也使用ReleaseDC发布DC。

但是当我使用VTune分析我的应用程序时,它会在GetDC上显示GDIResource泄漏。

m_hdc = ::GetDC(hWndDisplay[frameIndex]);
::SetStretchBltMode(m_hdc,STRETCH_DELETESCANS);
::StretchDIBits(m_hdc,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,0,0,imageWidth,imageHeight,rgbavpg,pTempBmpInfo,DIB_RGB_COLORS,SRCCOPY);
::ReleaseDC(hWndDisplay[frameIndex],m_hdc);

以下是相关代码:m_hdc id全局定义为HDC m_hdc;

void Display(unsigned char *rgbavpg,unsigned long imageSize, unsigned int imageWidth, unsigned int imageHeight, unsigned int frameIndex)
{

PBITMAPINFO pTempBmpInfo = NULL;
DWORD timespan;
int temp;

if ((IMAGE_WIDTH==imageWidth)&&(IMAGE_HEIGHT==imageHeight))
{
    frameNum++ ;
}
timespan = 1000/15;
DWORD diff = GetTickCount() - tickes[frameIndex];//lvm4;
tickes[frameIndex]=GetTickCount();
if (g_threadMarkedForStop[frameIndex] == TRUE ) 
{        
    return;
}
if(diff < timespan)
    { 
            Sleep(diff);
    }
if (FALSE == ::IsWindow(hWndDisplay[frameIndex])) 
{
    g_threadMarkedForStop[frameIndex] = TRUE
    return;
}

pTempBmpInfo = &m_bmpinfo[frameIndex];
if(pTempBmpInfo != NULL)
{
    pTempBmpInfo->bmiHeader.biWidth= imageWidth ;
    pTempBmpInfo->bmiHeader.biHeight= imageHeight;
}
else 
{
    g_threadMarkedForStop[frameIndex] = TRUE;

    return;
}
m_hdc = ::GetDC(hWndDisplay[frameIndex]);
     ::SetStretchBltMode(m_hdc,STRETCH_DELETESCANS);

if (true == fullscreen)                ::StretchDIBits(m_hdc,0,0,510,320,0,0,imageWidth,imageHeight,rgbavpg,pTempBmpInfo,DIB_RGB_COLORS,SRCCOPY);
else
{       ::StretchDIBits(m_hdc,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,0,0,imageWidth,imageHeight,rgbavpg,pTempBmpInfo,DIB_RGB_COLORS,SRCCOPY);
    //::SetDIBitsToDevice(m_hdc,0,0,IMAGE_WIDTH,IMAGE_HEIGHT,0,0,0,imageHeight,rgbavpg,pTempBmpInfo,DIB_RGB_COLORS);
}
::ReleaseDC(hWndDisplay[frameIndex],m_hdc);
}

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

尽管使用了成员变量m_hdc而没有使用局部变量,但这个代码本身看起来还不错。

您是否每次或仅在某些时候看到VTune标记泄漏?

当我看到你对IsWindow()和变量名称g_threadMarkedForStop的调用时,我想知道你是否正在进行多线程处理。可能是你在GetDC和ReleaseDC之间有时会破坏窗口吗?