In my application I am getting DC using GetDC and I am also releasing that DC using ReleaseDC.
But when I am profiling my application with VTune, it is showing a GDIResource Leak at GetDC.
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);
The following is relevant code: m_hdc id defined globally as 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);
}
Please help me to solve this problem.
Despite using a member variable m_hdc and not using a local variable this code by itself looks good.
Do you see VTune marking that leak every time or only sometimes?
When I see your call to IsWindow() and the variable name g_threadMarkedForStop I wonder if you are doing multithreading. Could it be that you sometimes destroy the window while being between GetDC and ReleaseDC?