Do I have to delete the "Default" brush for OnCtlColor "hbr"

Posted on 2006-04-03
Last Modified: 2009-12-16
The default brush for "OnCtlColor" is called "hbr",  but it is not a global declaration of this when I try to delete it in the "OnDestroy" function it is not usable.

Should I remove the default declaration of the brush and make it I can delete it via OnDestroy? Do I have to worry about it?

Any comments are appreciated. Thanks.
Question by:VirtueTech
    LVL 86

    Assisted Solution

    If that brush is contained in a global object, its destructor will take care about that when the program terminates. But, since your Q is quite broad, that is just an assumption that might not be too off.
    LVL 48

    Accepted Solution

    ObCtlColor usually looks like this:

    HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
       HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

       // some stuff

       return hbr;

    You don't need to release hbr. It is not created in your code, and you are not responsible for releasing it. If you don't call CDialog::OnCtlColor and return your own brush, you are responsible to release it.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    732 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

    21 Experts available now in Live!

    Get 1:1 Help Now