Click here to Skip to main content
15,924,193 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: I desperately need Pin
Christian Graus27-Feb-01 0:33
protectorChristian Graus27-Feb-01 0:33 
GeneralRe: I desperately need Pin
27-Feb-01 1:12
suss27-Feb-01 1:12 
GeneralRe: I desperately need Pin
l a u r e n27-Feb-01 1:08
l a u r e n27-Feb-01 1:08 
GeneralProblems with hooking events!! HELP !! Pin
TomK26-Feb-01 21:16
TomK26-Feb-01 21:16 
QuestionHow to make a deque from scratch in C++ Pin
Theo26-Feb-01 15:53
Theo26-Feb-01 15:53 
AnswerRe: How to make a deque from scratch in C++ Pin
l a u r e n26-Feb-01 21:29
l a u r e n26-Feb-01 21:29 
QuestionHow to resize the DataGrid columns ! Pin
26-Feb-01 12:23
suss26-Feb-01 12:23 
GeneralCPen leaking memory - ARGH !!!! Pin
Christian Graus26-Feb-01 12:13
protectorChristian Graus26-Feb-01 12:13 
I am trying to write a safe DC class, so I can avoid the hassle of keeping track of pens and brushes returned to me, as well as a one step create/select bitmap process.

Here is what I am doing:

CPen* CGDC::SafeSelect( CPen* pPen )
{
if (m_pOldPen)
{
return SelectObject(pPen);
}
else
{
m_pOldPen = SelectObject(pPen);
return m_pOldPen;
}
}

CBrush* CGDC::SafeSelect( CBrush* pBrush )
{
if (m_pOldBrush)
return SelectObject(pBrush);
else
{
m_pOldBrush = SelectObject(pBrush);
return m_pOldBrush;
}
}

m_pOldPen and m_pOldBrush are both NULL, I have stepped through and both functions behave as I would expect, as does the destructor:

CGDC::~CGDC()
{
if (m_pOldPen)
SelectObject(m_pOldPen);
if (m_pOldBrush)
SelectObject(m_pOldBrush);
if (m_pOldFont)
SelectObject(m_pOldFont);
DeleteDC();
}

Here's the thing. I tested it with this:

// for (int j = 0; j < 500; j++)
{
CGDC dc;

for (int i =0; i<500; i++)
{
CPen pen;
pen.CreatePen(PS_SOLID, 1, RGB(0,0,0));
/*CPen* pPen = */dc.SafeSelect(&pen);
CBrush brush;
brush.CreateSolidBrush(RGB(0,0,0));
dc.SafeSelect(&brush);
// dc.SelectObject(pPen);
}

}

Now, no matter which loop I comment in, if I comment out the SafeSelect on the pen it works fine. If I include the pen select and the call to SelectObject, it works fine. If I declare pPen here or not, if I call SafeSelect on the pen, it doesn't matter if I grab pPen here or not - unless I select it here, although it gets selected in CGDC, I lose about 30% of my GDI rsources when this code is called.

I have checked using GDI usage and the number of sequential ( in memory ) pens I leak varies, but is between 450 and 470. I don't leak any brushes. Can anyone tell me what is going on ? I have experimented with deleteobject, etc., but the fact is I seem to do the same thing inside the class and outside, but inside works for brushes, not pens.

Thanks

Christian

The content of this post is not necessarily the opinion of my yadda yadda yadda.

To understand recursion, we must first understand recursion.
GeneralRe: CPen leaking memory - ARGH !!!! Pin
Todd Wilson26-Feb-01 12:59
Todd Wilson26-Feb-01 12:59 
GeneralRe: CPen leaking memory - ARGH !!!! Pin
26-Feb-01 13:07
suss26-Feb-01 13:07 
GeneralRe: CPen leaking memory - ARGH !!!! Pin
Todd Wilson26-Feb-01 13:25
Todd Wilson26-Feb-01 13:25 
GeneralRe: CPen leaking memory - ARGH !!!! Pin
26-Feb-01 13:58
suss26-Feb-01 13:58 
GeneralRe: CPen leaking memory - ARGH !!!! Pin
Erik Funkenbusch26-Feb-01 13:40
Erik Funkenbusch26-Feb-01 13:40 
GeneralCDialog and dll Pin
Mel Stober26-Feb-01 12:10
Mel Stober26-Feb-01 12:10 
GeneralProblem displaying arbitrary Solid Brushs between machines Pin
Michael A Barnhart26-Feb-01 6:27
Michael A Barnhart26-Feb-01 6:27 
GeneralRe: Problem displaying arbitrary Solid Brushs between machines Pin
l a u r e n26-Feb-01 10:29
l a u r e n26-Feb-01 10:29 
GeneralRe: Problem displaying arbitrary Solid Brushs between machines Pin
Michael A Barnhart28-Feb-01 2:09
Michael A Barnhart28-Feb-01 2:09 
GeneralRe: Processes Pin
l a u r e n26-Feb-01 6:08
l a u r e n26-Feb-01 6:08 
GeneralRe: Processes Pin
l a u r e n27-Feb-01 1:02
l a u r e n27-Feb-01 1:02 
GeneralRe: Processes Pin
28-Feb-01 1:55
suss28-Feb-01 1:55 
GeneralUnique system id Pin
26-Feb-01 5:03
suss26-Feb-01 5:03 
GeneralStarting an App.Exe at Bootup time using NT Service Pin
trilok_soni26-Feb-01 2:34
trilok_soni26-Feb-01 2:34 
QuestionODBC drivers? Pin
Kannan26-Feb-01 0:52
Kannan26-Feb-01 0:52 
GeneralString table Pin
26-Feb-01 0:02
suss26-Feb-01 0:02 
GeneralRe: String table Pin
Tim Deveaux26-Feb-01 4:32
Tim Deveaux26-Feb-01 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.