在Windows Mobile开发中,显示图片时一般习惯使用voimage。但最近发现在调用voimage时是存在内存泄露的。
下面是voimage 的 Load函数的代码
在Windows Mobile开发中,显示图片时一般习惯使用voimage。但最近发现在调用voimage时是存在内存泄露的。
下面是voimage 的 Load函数的代码
BOOL CVOImage::Load(HDC hdc, LPCTSTR pcszFileName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if(m_hbitmap)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(GetFileName().GetLength() && GetFileName() == pcszFileName)
return TRUE; // Already Loaded
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DeleteObject(m_hbitmap);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(!m_hdc)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_hdc = CreateCompatibleDC(hdc);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SelectObject(m_hdc, hbitmap);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
BYTE szBuffer[1024] =
{0};
HANDLE hFile = INVALID_HANDLE_VALUE;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#ifdef _WIN32_WCE
HRESULT hr;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DecompressImageInfo dii;
#endif
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
hFile = CreateFile(pcszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hFile == INVALID_HANDLE_VALUE)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DWORD dwError = GetLastError();
return FALSE;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#ifdef _WIN32_WCE
// Fill in the 'DecompressImageInfo' structure
dii.dwSize = sizeof( DecompressImageInfo ); // Size of this structure
dii.pbBuffer = szBuffer; // Pointer to the buffer to use for data
dii.dwBufferMax = 1024; // Size of the buffer
dii.dwBufferCurrent = 0; // The amount of data which is current in the buffer
dii.phBM = &m_hbitmap; // Pointer to the bitmap returned (can be NULL)
dii.ppImageRender = NULL; // Pointer to an IImageRender object (can be NULL)
dii.iBitDepth = GetDeviceCaps(hdc,BITSPIXEL); // Bit depth of the output image
dii.lParam = ( LPARAM ) hFile; // User parameter for callback functions
dii.hdc = m_hdc; // HDC to use for retrieving palettes
dii.iScale = g_iScale; // Scale factor (1 - 100)
dii.iMaxWidth = g_iMaxWidth; // Maximum width of the output image
dii.iMaxHeight = g_iMaxHeight; // Maxumum height of the output image
dii.pfnGetData = GetImageData; // Callback function to get image data
dii.pfnImageProgress = ImageProgress; // Callback function to notify caller of progress decoding the image
dii.crTransparentOverride = ( UINT ) -1; // If this color is not (UINT)-1, it will override the
// transparent color in the image with this color. (GIF ONLY)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// del for yulong
// Process and decompress the image data
// hr = DecompressImageIndirect( &dii );
#endif
// Clean up
CloseHandle( hFile );
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HBITMAP hbitmapOld = (HBITMAP)SelectObject(m_hdc, m_hbitmap);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DeleteObject(hbitmapOld);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
BITMAP bmp;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
GetObject(m_hbitmap, sizeof(BITMAP), &bmp);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_dwWidth = bmp.bmWidth;
m_dwHeight = bmp.bmHeight;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
m_strFileName = pcszFileName;
m_dwResourceID = 0;
m_hmoduleResource = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return TRUE;
}
其中在CreateCompatibleBitmap时产生的内存位图的句柄被赋给了一个临时变量,使用完毕后却没有将其Delete,
我认为这里是有内存泄露了,导致每Load一次图片就是一次泄露
if(!m_hdc)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
m_hdc = CreateCompatibleDC(hdc);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, GetDeviceCaps(hdc, HORZRES), GetDeviceCaps(hdc, VERTRES));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SelectObject(m_hdc, hbitmap);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)