how avoid memory leaks?
from here: http://www.winprog.org/tutorial/bitmaps.html
i learned:
GetDC() - ReleaseDC()
BeginPaint() - EndPaint()
CreateCompatibleDC() - DeleteDC()
i must get the old selected object. select it when isn't needed and then delete it.
so seen these constructor and desctructor:
image(const int width, const int height)
{
if (isimgused==true)
delete img;
isimgused=false;
if(isGDIPLUSIniciated==false)
{
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
isGDIPLUSIniciated=true;
}
SelectObject(hdcimage, obj);
DeleteObject(btBitmap);
DeleteObject(obj);
DeleteDC(hdcimage);
hdcimage = CreateCompatibleDC(NULL);
btBitmap = CreateBitmap(width,height,1,32,NULL);
obj = SelectObject(hdcimage, btBitmap);
framecount=1;
imageheight=height;
imageweight=width;
}
~image()
{
if(isimgused==true)
delete img;
if(isGDIPLUSIniciated==true)
Gdiplus::GdiplusShutdown(m_gdiplusToken);
SelectObject(hdcimage, obj);
DeleteObject(btBitmap);
DeleteDC(hdcimage);
}
and these function:
void DrawHICONtoHDC(HDC hdc, HICON hIcon, int PosX=0, int PosY=0)
{
BITMAP BMInf;
ICONINFO csII;
GetIconInfo(hIcon, &csII);
GetObject(csII.hbmColor, sizeof(BMInf), &BMInf);
HDC MemDCExercising = CreateCompatibleDC(hdc);
HBITMAP hBitmap=CreateBitmap(BMInf.bmWidth ,BMInf.bmHeight,1,32,0);
HBITMAP oldbitmap =(HBITMAP) SelectObject(MemDCExercising, hBitmap);
DrawIcon(MemDCExercising,0,0,hIcon);
TransparentBlt(hdc, PosX, PosY, BMInf.bmWidth , BMInf.bmHeight, MemDCExercising, 0, 0,BMInf.bmWidth , BMInf.bmHeight,GetPixel(MemDCExercising,BMInf.bmWidth-1,BMInf.bmHeight-1) );
SelectObject(MemDCExercising,oldbitmap);
DeleteDC(MemDCExercising);
DeleteObject(oldbitmap);
::DeleteObject(csII.hbmColor);
::DeleteObject(csII.hbmMask);
DestroyIcon(hIcon);
DeleteObject(hBitmap);
}
what i'm doing wrong?
from a windows tool: the HDC, HBITMAP, HPEN, HBRUSH continue been created, instead delete the old one when isn't needed