Solved

Brush

Posted on 2000-03-18
10
326 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 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