Solved

Brush

Posted on 2000-03-18
10
323 Views
Last Modified: 2010-04-10
Experts:

I have a problem:
I create a brush in OnCtlColor() and return it later.This will make my control turn different color.
For example:

HBRUSH CMydialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
CBrush mybrush;
mybrush.CreateSolidBrush (RGB( 255, 0, 0 ));
if ( nCtlColor == CTLCOLOR_BTN )
{
pDC->SetBkColor(RGB(255,0,0));
pDC->SetTextColor(RGB(0,0,0));
return mybrush;
}

My confusion is since I create mybrush in the function above and mybrush should get destructed after return.I think the mybrush should die.How the system can use it to draw control color?
But in face it works!
And how about declare the brush in the if statement? Does it make difference?
Many thx.
0
Comment
Question by:binarydream
  • 5
  • 3
  • 2
10 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 125 total points
ID: 2631534
Hi,binarydream.
I think brush above get created in system source pool and only mybrush itself in the function stack.
And no destruction to set the m_brush of CBrush to null.
So , even CBrush is out of referecne.The brush handle still exists there and system can use.

Put in the if makes no difference as I figure.
Other experts please correct me if I'm wrong.

Hope helpful

Regards
W.Yinan
0
 
LVL 22

Expert Comment

by:nietod
ID: 2631602
I don't use MFC, but to the best of my knowledge, the CBrush class deletes the GDI brush when the brush object is destroyed.  (You can prevent this by detaching the GDI handle from the brush object using the detach() function.  When you do that the GDI brush won't be deleted whent he CBrush is destroyed.  But then you have another problem.  You will have a GDI mmemory leak.  You will becreating this GDI brush each ime the procedure is called and never deleting it.

The solutions is to store the handle to the GDI brush in a global variable--or to use a global/static CBrush object.  This global handle/object will hold the handle between "changes" to the brush.  each time the brush needs to change (color etc), you will delete the old handle and create a new one.  At the end of the program you must delete the last brush.  (If you use a global/static brush object, instead of a global brush handle, some of thise details are taken care of automatically.)
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2632568
Seems I'm an idiot on MFC.
nietod,looks like you misunderstand the question.
Regards
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 5

Expert Comment

by:Wyn
ID: 2632571
I think binarydream means the code works but he/she doesnt believe why it works.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2633051
I doubt the code works safely.  The brush will be deleted before the procedure returns and the procedure will return the handle to the deleted brush.   That is undeniably a mistake.  If the program manages to use that deleted brush successfully--and I don't know for sure that that is the case--then it is probably because the delete brush's data still remains in the GDI heap because it has not yet been used to store data for a new object.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2633400
Yes,doubt alot.
If it works,at least,binarydream,you creat brush very time the get called and gradually the system-resource get starved,then die.Hur?
0
 

Author Comment

by:binarydream
ID: 2636229
Adjusted points from 50 to 125
0
 

Author Comment

by:binarydream
ID: 2636230
Sounds a hard question.
However it works!!!!
Maybe wyn reminds me.
Thanks.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2636286
God!
I'm definitly wrong here,binarydream.
The brush object will be automatically destructed when function end.
You'd declare it as a class data member and initialize it in dialog initial...

I dont know why your code works but ,at least,I'm wrong here.

:(
0
 
LVL 22

Expert Comment

by:nietod
ID: 2636300
What is going on?  Why did you reject my answer?  Wyn's answer is definitely not right.  
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question