Solved

Brush

Posted on 2000-03-18
10
331 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

717 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