[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: MFC question: is it really necessary to call DeleteObject?

From: Stefan Küng <tortoisesvn_at_gmail.com>
Date: 2007-10-10 17:41:54 CEST

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
Received on Wed Oct 10 17:51:33 2007

This is an archived mail posted to the TortoiseSVN Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.