| |

VerySource

 Forgot password?
 Register
Search
View: 929|Reply: 7

A piece of code for a self-drawn button, why is there a memory leak?

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-16 20:30:02
| Show all posts |Read mode
Every time it runs, the memory will increase by 12 to 16 bytes. What's wrong?
Code:
void CShadowCtrl :: DrawItem (LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC * dc = GetDC ();
dcMemory.CreateCompatibleDC (dc);
CRect rc;
GetClientRect (&rc);

CBitmap bmp;
bmp.CreateCompatibleBitmap (dc, rc.Width (), rc.Height ());
dcMemory.SelectObject (&bmp);
Ranch
CBrush brush, * oldBrush;
CPen pen, * oldPen;

// draw BK
brush.Detach ();
brush.CreateSolidBrush (m_bkColor);
pen.Detach ();
pen.CreatePen (PS_SOLID, 1, m_bkColor);
oldBrush = dcMemory.SelectObject (&brush);
oldPen = dcMemory.SelectObject (&pen);
dcMemory.Rectangle (&rc);

// Draw Text
dcMemory.SelectObject (&m_textFont);
CString strText;
GetWindowText (strText);
dcMemory.SetTextColor (m_textCurrColor);
dcMemory.SetBkMode (TRANSPARENT);
dcMemory.DrawText (strText,&rc, DT_CENTER | DT_VCENTER | DT_WORDBREAK);

dc-> BitBlt (0,0, rc.Width (), rc.Height (),&dcMemory, 0,0, SRCCOPY);
dcMemory.SelectObject (oldBrush);
dcMemory.SelectObject (oldPen);
brush.DeleteObject ();
pen.DeleteObject ();
bmp.DeleteTempMap ();
bmp.DeleteObject ();
dcMemory.DeleteDC ();
}
Reply

Use magic Report

0

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-19 08:15:02
| Show all posts
You used
CDC * dc = GetDC ();
You should add
ReleaseDC (dc);
You try again to see if it works.
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-4-19 12:45:01
| Show all posts
I do n’t know what happened to the memory leak
But when I overload this function, rcItem, hwnd ... are all obtained through lpDrawItemStruct
Reply

Use magic Report

0

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-19 22:00:01
| Show all posts
A memory leak is when you apply to temporarily use the memory but not release it, especially in the ontimer event. Suppose you apply once a second so that your program should die soon.
Reply

Use magic Report

0

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-20 00:15:01
| Show all posts
E.g:
CPen * apen = new CPen; // Apply for temporary memory;
You should add
delete apen;
If not added, this is a memory leak.
I do n’t know if it ’s unreasonable to say that, I am also a rookie.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-4-20 01:15:01
| Show all posts
It ’s really a DC issue, thank you
DC with lpDrawItemStruct is fine
Reply

Use magic Report

0

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-4-20 11:15:01
| Show all posts
Just solve the problem, make progress slowly, we should become stronger after a long time.
Reply

Use magic Report

0

Threads

36

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 United States

Post time: 2020-4-23 20:00:01
| Show all posts
you forget to restore old bmp and old font.
CDC * dc = GetDC ();
dcMemory.CreateCompatibleDC (dc);
CRect rc;
GetClientRect (&rc);
The
CBitmap bmp;
bmp.CreateCompatibleBitmap (dc, rc.Width (), rc.Height ());
CBitmap * oldBmp = dcMemory.SelectObject (&bmp);
The
CBrush brush, * oldBrush;
CPen pen, * oldPen;
The
// draw BK
// brush.Detach ();
brush.CreateSolidBrush (m_bkColor);
// pen.Detach ();
pen.CreatePen (PS_SOLID, 1, m_bkColor);
oldBrush = dcMemory.SelectObject (&brush);
oldPen = dcMemory.SelectObject (&pen);
dcMemory.Rectangle (&rc);
The
// Draw Text
CFont * oldFont = dcMemory.SelectObject (&m_textFont);
CString strText;
GetWindowText (strText);
dcMemory.SetTextColor (m_textCurrColor);
dcMemory.SetBkMode (TRANSPARENT);
dcMemory.DrawText (strText,&rc, DT_CENTER | DT_VCENTER | DT_WORDBREAK);
The
dc-> BitBlt (0,0, rc.Width (), rc.Height (),&dcMemory, 0,0, SRCCOPY);
dcMemory.SelectObject (oldBrush);
dcMemory.SelectObject (oldPen);
dcMemory.SelectObject (oldFont);
dcMemory.SelectObject (oldBmp);
brush.DeleteObject ();
pen.DeleteObject ();
// bmp.DeleteTempMap ();
bmp.DeleteObject ();
dcMemory.DeleteDC ();
ReleaseDC (dc);
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list