[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tortoisesvn-dev
Subject:    Re: MFC question: is it really necessary to call DeleteObject?
From:       Stefan_Küng <tortoisesvn () gmail ! com>
Date:       2007-10-10 15:41:54
Message-ID: 470CF2C2.1090006 () gmail ! com
[Download RAW message or body]

Andreas Nicolai wrote:
> Hi again,
> 
> On Wed, 10 Oct 2007 01:42:34 -0400, Stefan Küng <tortoisesvn@gmail.com> 
> wrote:
> 
>> Yes, and that's one of the reasons MyGraph.cpp is slow and can run out
>> of GDI objects.
>> You have to delete those objects manually.
>> (btw: if you like to replace MyGraph.cpp, that would be great!)
> 
> Well, just hacked toghether a chart control a few months ago :-), but 
> using the Qt toolkit (see http://qnanwidgets.sourceforge.net). But even 
> if I were to reimplement that using MFC, this would take some time and 
> effort (too much for me right now), so I guess this can be a low 
> priority upgrade.
> 
> I like the idea of Hans-Werner and will create a wrapper class template 
> for all the MFC objects that need to call DeleteObject used in 
> MyGraph.cpp. How about something like:
> 
> AutoDeleteObject fontObj = AutoDeleteObject<CFont>();
> CFont * f = fontObj.object();
> 
> // when fontObj gets out of scope, the DeleteObject() function for the 
> CFont object is called automatically.
> 
> 
> So the class could be something like...
> 
> template <typename T>
> class AutoDeleteObject {
>     T m_theObject;
>     // prevent copying
>     AutoDeleteObject(const AutoDeleteObject& );
>     // prevent assignment
>     AutoDeleteObject & operator=(const AutoDeleteObject&);
> 
> public:
>     AutoDeleteObject() {}
>     ~AutoDeleteObject() {
>         m_theObject.DeleteObject();
>     }
> 
>     T * object() { return &m_theObject; }
> }
> 
> 
> Now I'm wondering why Microsoft doesn't have a simple class like that in 
> their libs? Or do they?

The problem with automatically deleting GDI objects is that you hide 
some problems: you can't really delete a GDI object that's selected into 
a device context. To *really* delete a GDI object, you must first select 
the previous GDI object into the DC, *then* delete the object.
By using auto-deleting objects, you cheat leak checkers because they 
then won't find the not-really-deleted object.

Stefan

-- 
        ___
   oo  // \\      "De Chelonian Mobile"
  (_,\/ \_/ \     TortoiseSVN
    \ \_/_\_/>    The coolest Interface to (Sub)Version Control
    /_/   \_\     http://tortoisesvn.net

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tortoisesvn.tigris.org
For additional commands, e-mail: dev-help@tortoisesvn.tigris.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic