Solved

Brush

Posted on 2000-03-18
10
320 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
Comment Utility
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
Comment Utility
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
Comment Utility
Seems I'm an idiot on MFC.
nietod,looks like you misunderstand the question.
Regards
0
 
LVL 5

Expert Comment

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

Expert Comment

by:nietod
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 5

Expert Comment

by:Wyn
Comment Utility
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
Comment Utility
Adjusted points from 50 to 125
0
 

Author Comment

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

Expert Comment

by:Wyn
Comment Utility
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
Comment Utility
What is going on?  Why did you reject my answer?  Wyn's answer is definitely not right.  
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now