天天看点

HICON泄漏

通常,我们使用的hicon对象只需用destroyicon后就不存在内存泄漏了,但是当我们使用geticoninfo后会发现程序gdi资源存在泄漏,原因是geticoninfo会产生2个hbitmap对象,即iconinfo结构体中的hbmcolor和hbmmask,需要deleteobject掉。切记切记!!!

cstring strvalue(_t(""));

hicon hicon = null;

hicon = extracticon(afxgetinstancehandle(), strvalue,

0);

if(hicon)

{

   crect rcicon;

   getclientrect(&rcicon);

   iconinfo icoinfo;

   geticoninfo(hicon,

&icoinfo);

   rcicon.left +=

rcicon.width()/2-icoinfo.xhotspot;

   rcicon.top +=

rcicon.height()/2-icoinfo.yhotspot;

   deleteobject(icoinfo.hbmcolor);

   deleteobject(icoinfo.hbmmask);

   drawicon(pdc->m_hdc,

rcicon.left, rcicon.top,

hicon);

   destroyicon(hicon);

}

继续阅读