由GDI+ 中Bitmap 得到HBITMAP 时遇到一个内存泄漏问题

潘祖记 2008-09-10 05:37:58
int COrcDlg::GetBmp()
{
HINTERNET internetopen =NULL;
internetopen=InternetOpen("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; InfoPath.1)",INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);

if (internetopen==NULL)
{
if(NULL != internetopen) InternetCloseHandle(internetopen);
return -1;
}
HINTERNET hConnection;
hConnection= InternetConnect(internetopen,SERVER,INTERNET_DEFAULT_HTTP_PORT,NULL,"HTTP/1.1",INTERNET_SERVICE_HTTP,0,0);
if (NULL == hConnection)
{
if(NULL != hConnection) InternetCloseHandle(hConnection);
return -1;
}
HINTERNET internetopenurl = NULL;
char szHeader[]= "Content-Type: application/x-www-form-urlencoded\r\n";
char cookie[] = "Cookie: ASP.NET_SessionId=yclxwe55xyfzwi453brbrv55";
internetopenurl = HttpOpenRequest(hConnection,"GET","/xxx/xxx/ValidateCodeImgShow0.3201143178969629.aspx","HTTP/1.1",NULL,0,INTERNET_FLAG_RELOAD| INTERNET_FLAG_NO_COOKIES,0);
if (NULL != internetopenurl)
{

if (TRUE == HttpAddRequestHeaders (internetopenurl,szHeader,strlen(szHeader),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD))
{
if(TRUE ==HttpAddRequestHeaders (internetopenurl,cookie,strlen(cookie),HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDREQ_FLAG_ADD))
if(TRUE == HttpSendRequest(internetopenurl,NULL,0,NULL,NULL))
{

IStream* pStmBmp = NULL;
HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, 1024);
if (hMemBmp != NULL)
{
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
if (pStmBmp == NULL)
{
GlobalFree(hMemBmp);
return FALSE;
}

}

while(1)
{
DWORD byteread=0;
BOOL internetreadfile;
char buffer[1024];
memset(buffer,0,sizeof(buffer));
internetreadfile=InternetReadFile(internetopenurl,buffer,sizeof(buffer),&byteread);
if(byteread==0)
break;
ULONG pcbWritten;
pStmBmp->Write(buffer,sizeof(buffer),&pcbWritten);
}

Bitmap pic(pStmBmp,NULL);
Gdiplus::Color clr(0xFF,0xFF,0xFF); // (only blue component gets used)

HBITMAP bmp;
if (Gdiplus::Ok == pic.GetHBITMAP(clr, &bmp))
{
m_bmp.SetBitmap(bmp);
::DeleteObject((HGDIOBJ)bmp); //这个地方必须要,不然就会内存泄漏
}




pStmBmp->Release();
GlobalUnlock(hMemBmp);
GlobalFree(hMemBmp);

//AfxMessageBox(strsn.c_str());
InternetCloseHandle(internetopenurl);
InternetCloseHandle(internetopen);
//

}


}
}
else
{
if(NULL != internetopenurl) InternetCloseHandle(internetopenurl);
if(NULL != internetopen) InternetCloseHandle(internetopen);
return -1;
}

return 1;
}
...全文
419 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
潘祖记 2008-09-16
  • 打赏
  • 举报
回复
验证大小就只有456字节
xsc2001 2008-09-14
  • 打赏
  • 举报
回复
感觉你分配的内存不够,HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, 1024);从这代码看,你只分配了1K大小。你应该从http请求的信息头中得到Content-Length的值后,根据这个值分配相应大小的内存。

16,550

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧