请问我这段代码,为什么会溢出。
void PDC::ShowReverseTrace(void)
{
//get the file path
TCHAR moduleDir[260];
TCHAR PicPath[260];
GetModuleDir_pdc(moduleDir);
wcscpy(PicPath, L"\");
wcscat(PicPath, moduleDir+1);
//set the camera visual degree
if(isCamerahighAngle)
wcscat(PicPath,RC_Array[traceIndex]);
else
wcscat(PicPath,RC_Array_EX[traceIndex]);
//display the picture by path ,and decode the file (if not bmp )
{
IImage *pImg = NULL;
ImageInfo imgInfo;
int pix_format;
RECT bmp_rect;
IBitmapImage *pBitmap;
BitmapData bmpData = {0};
HBITMAP hbmp = NULL;
int bits_pixel;
BOOLbRet = FALSE;
BITMAP bmpinfo;
if(m_hdcTrace == NULL)
{
if (hbmp)
{
DeleteObject(hbmp);
}
}
//by source display
//hbm = (HBITMAP)LoadImage(hInstance, lpstrResID, IMAGE_BITMAP, 0, 0, 0); //from resource
{
//init COM,isComInit_Pdc is a flag make init action do only once
if(FALSE==isComInit_Pdc)
{
HRESULT hRet;
if (FAILED(hRet = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
printf("CoInitializeEx failed!");
return ;
}
if (FAILED(hRet = CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void**)&g_pImagingFactory_pdc)))
{
printf("CoCreateInstance failed!");
return ;
}
isComInit_Pdc = TRUE;
printf("wince com init once\r\n");
}
//image decode
HDC temphdc = ::GetDC(NULL);
bits_pixel = GetDeviceCaps (temphdc, BITSPIXEL); //get the device color depth 16 or 32
::ReleaseDC(NULL, temphdc);
UINT32 t_u4Result = g_pImagingFactory_pdc->CreateImageFromFile(PicPath,&pImg);
if (t_u4Result != S_OK || pImg == NULL)
{
PRINTF_WRONG("t_u4Result != S_OK || pImg == NULL")
return;
}
pImg->GetImageInfo(&imgInfo);
pix_format = (bits_pixel==16) ? PIXFMT_16BPP_RGB565 : PIXFMT_32BPP_ARGB;
g_pImagingFactory_pdc->CreateBitmapFromImage(pImg,imgInfo.Width, imgInfo.Height,pix_format,InterpolationHintDefault,&pBitmap);
SetRect(&bmp_rect, 0,0,imgInfo.Width,imgInfo.Height);
pBitmap->LockBits(&bmp_rect, ImageLockModeRead, pix_format, &bmpData); //get the pic binary data to bmpData
hbmp = CreateBitmap(imgInfo.Width, imgInfo.Height, 1,bits_pixel, bmpData.Scan0); //从位图数据bmpData.Scan0创建HBITMAP
}
if(hbmp == NULL)
{
printf("SHLoadDIBitmap %S failed!!\n",PicPath);
m_hdcTrace = NULL;
if (hbmp)
{
DeleteObject(hbmp);
}
}
//获得bitmap大小信息
GetObject(hbmp,sizeof(BITMAP),&bmpinfo);
m_rcTrace.left = 0;
m_rcTrace.top = 0;
m_rcTrace.right = bmpinfo.bmWidth;
m_rcTrace.bottom = bmpinfo.bmHeight;
m_hdcTrace = CreateCompatibleDC(NULL);
SelectObject(m_hdcTrace, hbmp);
if(NULL!=hbmp)
{
DeleteObject(hbmp);
}
if(NULL!=pImg)
{
DeleteObject(pImg);
}
if(NULL!=pBitmap)
{
DeleteObject(pBitmap);
}
bRet = TRUE;
if(!bRet)
{
return ;
}
}
//if( !bRet1)
// return ;
if(FALSE==isRevTraceInit)
{
UIHdc *pHdc=NULL;
UI_HDC_DATA hdc_data;
RECT rt={0,0,800,480};
hdc_data.hdcRect=rt;
hdc_data.szHdcName=L"HDC_TRACE";
pHdc = CreateHdc(g_hinst,&hdc_data);
if(NULL!=pHdc)
{
pHdcTrace=pHdc;
}
UI_BUTTON_DATA button_data;
button_data.iPosX =100;
button_data.iPosY =20;
button_data.CallFunc =OnTouchButton;
button_data.szButtonName=L"TRACE_BUTTON";
button_data.wButtonBmpDownResID=L"IDB_BITMAP_TRACE_BUTTON_FREE";
button_data.wButtonBmpResID=L"IDB_BITMAP_TRACE_BUTTON_SEL";
pTraceButton=CreateButton(qtQpcpp_inst, &button_data);
if(NULL!=pTraceButton)
{
pTraceButton->SetVisible(FALSE);
}
isRevTraceInit=TRUE;
printf("trace dc and button init once\r\n");
}
if(NULL!=pHdcTrace)
{
pHdcTrace->SetVisible(TRUE);
BitBlt(pHdcTrace->GetHDC(), 0,0,m_rcTrace.right-m_rcTrace.left,m_rcTrace.bottom-m_rcTrace.top,m_hdcTrace,0,0,SRCCOPY);
pTraceButton->SetVisible(TRUE);
}
} DDraw?从文件路径显示图片,内存溢出 wince
[解决办法]
1、m_hdcTrace如果没有在其它地方DeleteDC,那就存在泄露
2、我如果没记错,bmp如果SelectObject到hdc,实际是无法真正释放的,需要先把oldbmp选回
3、pImg和pBitmap是COM对象,释放应该使用其接口Release
另,你既然已经使用IImagingFactory进行图片解码,那完全可以使用IImage的接口Draw将图像数据复制到hdc中,不需要增加如此多的额外操作