?
Solved

Brush

Posted on 2000-03-18
10
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.
Suggested Courses

801 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