Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory leak with CDC? 1

Status
Not open for further replies.

mirr2001

Programmer
Mar 29, 2002
24
DK
I am currently working on an ActiveX control, which shall write strings on the screen. Every letter is represented by a CBitmap (about 10*12 px * 16 colours) object. Every letter is a member-variable (CBitmap letterA, letterB...)and is initialized in the constructor (LoadBitmap(ID)). Then all the labels on the control have their own buffer dc (also as members initialized in the constructor).Actually everything works fine, except a memory leak every time this loop is called (toWrite is a CString object):


CDC* buffer= &m_dcFirstLabel;//or &m_dcSecondLabel etc.
for(int i=0; i<toWrite.GetLength(); i++)
{
CBitmap* bmp= &letterA;//no wild pointers!
switch(toWrite.GetAt(i))
{
case 'A':{bmp= &letterA;};break;
case 'B':{bmp= &letterB;};break;
case 'C':{bmp= &letterC;};break;
case 'D':{bmp= &letterD;};break;
//all other letters
}
CDC bmpDC;
bmpDC.CreateCompatibleDC(GetDC());

CBitmap* old= bmpDC.SelectObject(bmp);
buffer->BitBlt(posToWrite, 0,bmp-> GetBitmapDimension().cx, bmp->GetBitmapDimension().cy, &bmpDC, 0, 0, SRCCOPY);
posToWrite+=bmp->GetBitmapDimension().cx;

bmpDC.SelectObject(old);
bmpDC.DeleteDC();
}//for end

Could anyone give me a hint, were the leak could be, cause I loose about 4KB every time I call this function with a string of 5 characters.
 
I had a similar problem a few months ago. Try this:

CDC bmpDC;
CDC* pDC = GetDC();
bmpDC.CreateCompatibleDC(pDC);
ReleaseDC(pDC);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top