Introduction

Windows resources, like GDI handles, are hard to manage correctly without
creating leaks. This article shows how smart pointers can simplify this, and
almost eliminate a major source of errors. It continues my previous article,
Smart Pointers to boost your code,
providing a practical example for using shared_ptr. (If you are not
familiar with boost::shared_ptr, you should read it now!)

Update: While the first version was more or less like "concept art", I
brushed up the sources a bit to make the more useful. I added a
reset() function that sets the HandleRef to 0, brushed up the
sources a bit and added a special implementation for HDC's. Also, I fixed some
snippets here, and added a few things to the article.

Contents

Background will examine the problem in
detail, and discuss the common solutions.

Many resource handles encountered in Win32 programming do not fit an
object-oriented environment very well. Here is a list of problems:

How you acquire the handle determines if you have to call some cleanup
function on it. If you use CreateFont, you have to call
DeleteObject when you no longer need it. However, if you got the
HFONT from a Window, you must not delete the font.

There is no way to tell from a handle whether we should delete it or have to
leave it alone.

There are many handle types, with many different Delete/Release functions,
which must be matched exactly.

Handles have "pointer" semantics, i.e., copy constructor and assignment
create only a new reference to the actual resource. This is desirable for
performance reasons, however, this makes it complicated to use RAII in an object
oriented environment.

If you have a function returning such a handle, you must at least specify if
and when to release the handle. Things get much more complicated if the handle
is a member of the class. Take a look at this innocent code snippet:

Keeping a "bool deleteTheFont" flag together with the font
handle. (I worked with std::pair<HANDLE, bool> for a while,
but this is still a pain.)

Count the references to the font. (E.g., the copy constructor would have to
increment the reference count.)

Use some kind of internal reference counting. (This works for File, Event,
Thread, and many other handles, using DuplicateHandle. However,
this is another can of worms opening up. No such luck with GDI handles.)

The nature of handles makes them prime candidates for a reference counting
mechanism:

They act as "references to resources", but the resources themselves
cannot/should not be copied.

The same resource is reused in many places, and must not be deleted as long
as someone is using it.

It should, however, be released as soon as possible, to "play nice" with the
system resources.

These resources discussed here don't reference each other, so circular
references should virtually never appear.

Further, we would like to keep the possibility of "unmanaged" handles (i.e.,
it is not deleted automatically). This flag should be set when the smart pointer
is created (close to where we acquire the resource, because this is the place
where we know how to treat it).

The line (A) is the "magic" one: here we initialize the CFontPtr
as usual, but specify that the object should be deleted using
delete_HFONT when the last reference is gone.

Now, we can make heavy use of CFontPtr: we can use it as return
value from functions. We can have it as class member, and the default copy
constructor, assignment operator, and destructor do exactly what they
should.

Do you remember the first rule for using smart pointers? Put the resource
into a smart pointer as soon as you get it. This rule holds up here as well:
because when we get the font handle, we know exactly if it should be deleted or
not. The smart pointer will carry around this flag, and automatically "do what
we want".

There are some problems even this solution cannot solve:

Someone could delete the font behind our back.

We could create the font pointer with deleteOnRelease = false,
and then forget to delete it ourselves.

But such is life in C++, you are always free to shoot yourself in the foot
you like most.

Some problems, however, can be solved better:

Checking for a "null" font is tedious: we have to check both the smart
pointer, and the actual font:

if (m_fontPtr && *m_fontPtr) // do we have a font?

We must dereference the smart pointer any time we want to access the actual
object.

To have to write three entities (deleter, smart pointer
typedef, Create function) for any other handle type we
would like to support.

The public interface is reduced to the bare minimum. (A "good thing".)

The CreateFontPtr function has become the constructor.

An automatic cast operator allows both the if (font) test, and using the
class in a place where a HFONT is expected.

The deleter remains an associated function (not shown here).

The class was renamed from "Ptr" to "Ref", since
it syntactically cast more like a reference than a pointer.

The constructor does not specify a default parameter. This was done since no
value is the "obvious default". Further, it makes the construction explicit (so
the explicit keyword isn't really needed).

I typedef'ed the boost pointer inside the class. It occurs in
quite a few places, the typedef makes the code easier to read, but
"outside" the class, no one really needs it.

Another note: Now would be a good time to make the simplification used
below. However, I'd like to go the "safe & clean way" a bit
further.

Second, we can make both the Handle type and the Deleter a template
parameter. It's simple for the Handle, but some compilers can't handle a
function as template argument. The standard solution is to turn the Deleter
function into a functor - that is, a class that overloads
operator().

Specialized, more lightweight implementation

There are two things still bugging me:

The deleters still need to remember the "delete p" part.

I would like to avoid the heap copy of the resource handle if possible.

The first problem could be solved with another helper template, but that
wouldturn the actual type of CFontRef into something like
boost::shared_ptr<HFONT, GenericDeleter<
DeleteObjectDeleter<HFONT> > >. I see no solution for the
second in a generic approach.

However, both solve themselves when using some internal knowledge about
Windows: all resource handles in Windows can be represented by a void
*, and we can use a cast to get the strictly typed handle. This is so
deeply rooted in the Win32 API (in fact, if you #undef the
STRICT macro, most handle types are declared as void
*), that we won't see a change till .NET takes over completely. Further,
boost::shared_ptr can use void as template
argument.

(I'll repeat a few things here for the impatient who skipped over all
that boring explanation stuff)

CHandleRefT is a template class implementing a counted reference
to a Windows resource handle. Usage rules are similar to a reference counted
smart pointer. It is used to implement a variety of Windows Resource handles,
like:

HIMAGELIST ==> CImageListRef

HMENU ==> CMenuRef

HANDLE ==> CHandleRef

HBITMAP ==> CBitmapRef

HBRUSH ==> CBrushRef

HPEN ==> CPenRef

Template parameters:

HDL: type of the resource handle (e.g., HFONT).

DELETER : a functor releasing the resources of Handle of type
HDL (e.g., a functor calling DeleteObject). The handle is passed as
void *. A custom deleter can be passed to the constructor.
However, DELETER is not a template parameter of the class (which avoids template
propagation)

Automatic vs. manual handles

When constructing a HandleRef from a raw handle, you pass a
bDeleteOnRelease flag indicating if the handle should be released
automatically when the last reference to it goes out of scope.

For an automatic handle, assign the raw handle to a HandleRef
immediately after construction, specifying bDeleteOnRelease=true.
Then, pass it around only as HandleRef. This guarantees the handle
is deleted when it is no longer used.

For a manual handle, that you will delete yourself manually, or that must not
be deleted, specify bDeleteOnRelease=false.

The idea here is that you can pass around, copy, store, or return a
HandleRef, it remembers its deletion policy.

Guidelines

Utility functions that merely receive and use a resource handle (without
storing it) may use the raw handle as argument. However, when the Handle is
stored (e.g., as a class member) or used as a return value from a function, a
HandleRef is recommended.

As an example, the CImageListRef class has the following
members:

Constructor

CImageListRef(HIMAGELIST il, bool deleteOnRelease)

Initializes a new CImageListRef.

il [HIMAGELIST]: Image list to hold

deleteOnRelease [bool]: if true, the image List il
is destroyed when the last reference to it (made through the
CImageListRef instance) goes out of scope. The correct destroy
function (ImageList_Destroy) is used.

MFC tried to solve the problem with its own wrappers. Unfortunately, they
chose to wrap the first option from the "possible solutions" above: no copy
functionality, but they pass around CFont *. This might have been a
good design decision back then, but now, it is just a pain. Consider a function
returning a CFont *. Now, there are two objects that need to be
released correctly: the Windows HFONT, and the CFont C++ object. Should you:

Delete the CFont * when you are done with it, because it was
allocated dynamically?

Not delete the CFont *, but use it only until some other class
is destroyed (because the other class "holds" the font)?

Not delete the font, but use it only in the current message handler (because
it's a temporary MFC object that is deleted in the next OnIdle
call)?

Detach the HFONT from the CFont * before deleting
it (because you have to get rid of the MFC object, but the Windows resource is
still in use somewhere)?

When turning UI code into general routines and classes, I regularly stumble
over all these four scenarios. It is not a pleasant experience, I can tell you -
so I end up using raw resource handles instead, to have "only the normal
problems".

Further, I like UI code not to be dependent on MFC, if possible. The best
code IMO is a library that relies solely on the Win32 API interface, and does
not put any requirements on the framework.

Device Contexts are the most complicated resource I've come across: There are
two cleanup functions: DeleteDC and ReleaseDC,
and the latter needs an additional parameter (the HWND the DC was
acquired from).

CDCRef is implemented as separate class, since I didn't want the complexity
of HDC to "creep" into the Other HandleRef classes. The main difference is the
constructor - instead of a flag, you directly pass the deleter:

CDCRef(hdc, CDCRef::Null()) // a CDCRef that doesn't get released automatically
CDCRef(hdc, CDCRef::Delete()) // a CDCRef to be released by DeleteDC
CDCRef(hdc, CDCRef::Release(hwnd)) // a CDCRef to be released by ReleaseDC

For ReleaseDC, the HWND is passed as parameter to the deleter. Again, we
associated all information required for cleanup with the object when it is
constructed, which is a design principle of HandleRef.

Further, CDCRef provides static member functions that wrap the Win32 API
functions that acquire a HDC. For example,
CDCRef::GetDC implements the GetDC Win32 API function,
but returns a CDCRef.

The solution provided here is not new. The idea of the article is to show
that the solution becomes very simple when using an appropriate library (about
20 lines of actual code), and how such a solution would be developed from the
initial idea to use shared_ptr.

Now, How good is the solution?

You need to know how Win32 Resources are managed

You need to know how smart pointers work

You have a slight overhead over plain Win32 code: a separately allocated
object (8 Bytes) for each managed handle, and the cleanup call is through a
function pointer.

The second is not an issue: smart pointers are such a fundamental technique
that you shouldn't be caught without. The last might be an issue when handling
lots of resources. However, such an application likely benefits most from
automated resource management, and you can keep performance under control with
using a raw handle (or a CHandleRefT<> const &) where
appropriate.

The first actually reflects a design choice: CHandleRef does not
isolate you from the underlying API, but makes it transparent and easier to use.
(MFC, in contrast, isolates you very well in the default case, and fails
misderably in all others). Additional advantage: CHandleRef is not only library
independent, it also integrates easily with other libraries.

Is a "better" solution possible?

Definitely. You still can do things wrong: release an object while it is
still selected into a device context, specify the wrong cleanup policy, or
delete the handle while it is still in use. However, a completely safe solution
would require to wrap the entire GDI API: all functions creating or using an GDI
object.

CDCRef actually show the difference between an open
wrapper (allowing access to the underlying API handles) and a close
wrapper (denying the same access).

To make CDCRef construction "foolproof" the CDCRef
constructor would have to be protected, so they can be constructed only through
dedicated functions that initialize correctly. But this requires wrappers for
all functions acquiring a DC. Further, to make CDCRef completely
foolproof, one also would have to wrap all functions accepting a DC, and remove
the operator HDC() from the class. The open wrapper leaves you some
responsibility, but you have no problems if I forgot to provide a wrapper (or
the original API is extended).

Conclusion and Thank You

Thank you all for the encouraging feedback! It is great to see the time to
write the article was well spent.

For a more formal conclusion: We have seen that

Resource Handles, as many other objects, can have a variety of destruction
policies, which are not "visible" from the handle itself

The deletion policy should be "attached" to the handle when the handle is
acquired

boost::shared_ptr provides the features to make an
implementation simple

by using some platform-specific knowledge, we can make the solution more
efficient, and less complex in terms of code, while keeping the original
interface

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

Peter is tired of being called "Mr. Chen", even so certain individuals insist on it. No, he's not chinese.

Peter has seen lots of boxes you youngsters wouldn't even accept as calculators. He is proud of having visited the insides of a 16 Bit Machine.

In his spare time he ponders new ways of turning groceries into biohazards, or tries to coax South American officials to add some stamps to his passport.

Beyond these trivialities Peter works for Klippel[^], a small german company that wants to make mankind happier by selling them novel loudspeaker measurement equipment.

Where are you from?[^]Please, if you are using one of my articles for anything, just leave me a comment. Seeing that this stuff is actually useful to someone is what keeps me posting and updating them.Should you happen to not like it, tell me, too

I've said that before some posts below - my problem is that the "delete it or not" flag is a template parameter, meaning I can't assign a CMenuT<true> to a CMenuT<false> - so I can't decide about policy only when I create the handle. They work very well as "scope guards" - i.e. making sure some handle is destroyed when the wrapper leaves scope. I aimed at a bit more than that.

Anyway, thanks for your feedback As mos feedback shows, this is only a partial and/or incomplete solution - this is also my experience using them.

We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CPMy first real C# project | Linkify!|FoldWithUs! | <a href=http://www.cherea.de/sighist>sighist

The kernel has a reference count for handles (all handles) that you ask it to create. The problem is that many people don't pass handles correctly. If you are going to do anything that you think should require an increment to the reference count, you should use DuplicateHandle (and a subsequent call to the appropriate release method). Adding another reference count to it will just decrease the performance of your applications.

As an aside, MFC knows about the kernel reference counting and hence its wrappers handle them properly (in most cases).

If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

(1) Not all "Handle-Like things" have an DuplicateHandle. One example: Registry keys (not technically kernel handles, you can open another handle to the same key, but you lose the access rights of the original HKEY)

(2) The solution is generic for all pointer-sized handles. Less code, less test cases, less things to know. A custom implementation for fonts would be very expensive (HFONT -> LOGFONT -> HFONT, or is there a better one?)

(3) The kernel reference count isn't available (directly or indirectly) in user mode.

E.g. DuplicateHandle has two drawbacks: It returns a new handle value (I tried XP SP2, file handle, DUPLICATE_SAME_ACCESS). So we can safely assume using DuplicateHandle takes longer and eats additional resources per copy. Further, the copy operation could fail, which must be handled.

using CHandleRef in turn has the following drawbacks:- additional tracking object allocated on the heap for each handle (but not for each copy). That makes CHandleRef unsuitable if you rapidly create and destroy thousands of handles. It's the one major drawback I admit freely - twice the size. No real problem- pass by value is bit more expensive than passing a handle by value.- additional dependency

In the end I think it's 90% abot taste and style. Only with the remaining 10% I'd insist that class/library interfaces using handle wrappers like this are more elegant and usable, and once "reference counted" us understood, I can safely forget about so many things, which is my personal holy grail of software design.

At the sides, MFC in most cases opts to make the wrappers uncopyable. A proper decision design wise, but damn inconvenient.

Returning is a form of passing. And generally speaking, you shouldn't be storing handles. There is a slim chance (depending on the type of handle) that the OS can decide to move it. If you kept the original handle, you will end up with a very random (meaning extremely difficult to debug) crash.

peterchen wrote:

(1) Not all "Handle-Like things" have an DuplicateHandle.One example: Registry keys (not technically kernel handles, you can open another handle to the same key, but you lose the access rights of the original HKEY)

Anything that is equivalent to HANDLE can be used in DuplicateHandle. In fact, some of the APIs for other types of handles actually make calls to DuplicateHandle. Yes, it will be less efficient than passing a reference to your handle value, but it will also be safer in some cases (actually, almost all cases).

peterchen wrote:

(2) The solution is generic for all pointer-sized handles. Less code, less test cases, less things to know. A custom implementation for fonts would be very expensive (HFONT -> LOGFONT -> HFONT, or is there a better one?)

Your intensions aren't the problem. Unfortunately, when it comes to kernel objects, you can't treat them like any other pointer. If you don't follow the proper steps for creation/deletion for a given type of kernel object, you will easily cause resource leaks that will bog down your entire system (until you reboot). Your choices for things like this are to read the documentation very carefully and wrap those steps into your own custom classes, or to use libraries that already do it for you. MFC does the font stuff fairly well (though, it does take some getting use to).

peterchen wrote:

(3) The kernel reference count isn't available (directly or indirectly) in user mode.

That is how it should be. You shouldn't care what the reference count is. The only time you really need to look at it is when you are debugging, and you can get to it then (through the debugger).

peterchen wrote:

E.g. DuplicateHandle has two drawbacks:It returns a new handle value (I tried XP SP2, file handle, DUPLICATE_SAME_ACCESS). So we can safely assume using DuplicateHandle takes longer and eats additional resources per copy.Further, the copy operation could fail, which must be handled.

DuplicateHandle (and its equivalents) basically create a new pointer to the resources. You aren't going to use this method for things like passing the handle into a function to operate on the handle (say, to use it to draw something to the screen). An example of when you want to use it is when you want to have 2 threads drawing to the screen (each thread would have its own copy of the handle).

If the copy operation fails, it means something in the system went very wrong (e.g. you ran out of resources).

I don't question your motives behind this article (I actually like the direction you are thinking), it is just wrapping something that is already done by the OS (the reference counting) creates duplicate functionality and increases complexity (what happens if your reference count gets out of synch with the OS's?). Another way you could wrap handles is to create a method that automatically dupilcates it for you and returns a new wrapper (you probably wouldn't want to do that in the copy-constructor nor the copy-assignment methods though ... that would prevent you from passing the class by value, which you may want to do sometimes).

If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

The shared_ptr references don't need to be in sync with the kernel. They key is the usgae guide: create, and assign, e.g. I'm using:

CHandleRef OpenLogFile() { ... }

The OpenLogFile method opens the handle and immediately assigns it to a CHandleRef. The raw handle is now "owned" by CHandleRef, meaning I must make sure it is not closed manually, or stored in raw form. As long as I guarantee this, my reference count will be on top of a single reference to the actual file object (I'm not sure how familiar you are with COM, but the same mechanism is used for reference counting across an apartment boundary.)

I agree that this requires some bit of "training", and maybe I find it easy only because I know smart pointers inside out from years of COM. But this pattern (both the idea and the code) is highly reusable, e.g. just two days ago for enumerating firewire devices. Two lines of code for an exception safe HDEVINFO (and another shared_ptr for the dynamic sized structures used in hardware enumeration)

I'm not trying to brush away your comments (even if it may sound like), I often notice that I "learn" something from such a discussion only a few weeks (even months) later.

Zac Howland wrote:

you shouldn't be storing handles.

Sorry, I can't follow you here. The "behavior" of handkles is well defined. Of course I might accidentally close a handle behind the stores back etc. but code can't protect us from stupidity anyway.

What I'm trying to do is making write good code simpler. Most Windows bugs are in resource management. By associating the cleanup function with the handle there's one thing less to remember.

I've always been unhappy with the MFC solution, particulary because it requires you to read lot of MFC documentaiton carefully, and it doesn't interoperate with other libraries very well.

Zac Howland wrote:

If the copy operation fails, it means something in the system went very wrong (e.g. you ran out of resources).

Agreed, if the DuplicateHandle faisl, you can probably shut down the app anyway. But error handling is an application aspect that cannot be decided by the library (CHandleRefT doesn't include any error handling. but it makes it much much easier)

- ImplementationWhy do you use boost::shared_ptr? Seriously, all you need is a reference counter that indicates if a resource must be destroyed in the destructor.

-DesignResource handling is 'partly managed' which is error-prone: Ownership is controlled by a flag that has to be set by the user. On the other hand, managed resources are converted automatically to unmnaged handles by operator HDL().

There are definitely questionable design choices, the following is the motivaiton for taking them, I don't think they are "better" than some other options.

(1)because boost provides exactly what I need, and reduces the implementation effort. You need: - A class / struct for the shared resource itself - The "smart pointer" class (whatever it is) - A "plugin point" for the custom resource deleter

This takes me to three entities, one of which is visible to the user and needs utmost care in handling (I've written smart pointer like stuff before, it's nothing I wouldn't like to have thorughly tested in production code, and a C++ guru should look for pitfalls)

boost::shared_ptr provides just that: holding the reference count, and the handle, a fully tested reference counting pointer, and a custom deleter that is NOT a template parameter (which is important to avoid template infection of the using application)

(2) It was not a design goal to completely isolate the client from GDI resource handling. I've pondered it a long time, and concluded it is not possible wihtout wrapping huge chunks of the windows API.

Rather, I wanted: - a solution that can be integrated with any other library that can work somehow with raw handles. - SIMPLIFY common resource handling problems - Move the "How to delete" decision to the point where the resource is acquired - low amount of code

Using the library can simplify resource handling, if you understand reference counted entities (not too simple, but very common), and understand Win32 GDI resource handling (which is IMO a necessity, unless you wrap the entire GDI API).

implicit cast to HANDLE type:I was uneasy about the cast to raw handle, it's a tradeoff between comfort and bulletproofness. I opted for comfort, that's all. The alternative would be: - a cast to "unspecified_bool_type" (as boost does) for the "Handle zero" check - a HANDLET get() const
memberI would prefer the latter if HANDLET could be a complex type. However, we are basically using void * / unspecified_type * here. In this case, I believe, the implicit cast is safe.

Also, since I want to integrate with "anything that takes raw handles", I cannot protect the client from accidentally deleting the raw handle with the wrong function or at the wrong time.

The core implementation is less than 2 dozen lines (not counting curly braces)

I think I stated this before - but the idea of this article was to privde a practical example for my previous one, and see how/if it could solve a long-nagging problem (reference counting for handles).

How does this object play with SelectObject, a function which changes the delete requirements of an object.

For Example

CSmartPenHandle pen( ::CreatePen( PS_SOLID, 1, RGB(0,0,0) ) );// now the pen should be deleted by SelectObject

// I think that ref counting smart pointers are good, but not for DCs,// there too complex a resource for that. You don't want to leave// them lying around for sure and ref counting might let that happenHDC hdc = ::GetDC( hMyWnd );

::SelectObject( hdc, pen.GetWin32Handle() );// pen now mustn't be deleted. Especially if hdc is a class or window dc.// if it is neither then typically you'd select the return value back in,// but if you don't then ReleaseDC deletes the pen, and your destructor// will fail.

Note that this is just a thought excersise. The example is great as it stands, but i'm simply trying to give an example of why GDI objects in particular don't really lend themselves to smart pointers.

I was considering this when writing, but I finally left it out for several reasons.

You could use a "DC Selector" helper (see below), but ultimately, I don't think any amount of code can circumvent all possibilities to shoot yourself in the foot. In the end, you will have to follow some rules.

The basic idea would be to (a) make sure the object is deselected (e.g. using SaveDC/ReleaseDC, possibly with a scope guard to be safe), and (b) hold a strong reference to the object until it is deselected

I wouldn't even use HandleRef for temporary objects created in the WM_PAINT itself, they original idea was for GDI resources you "keep around" instead of reinitalizing them again and again. But the following could help a bit (but it's just a rough outline):

(It would require special CDC support, since DC's come with two destructors - ReleaseDC and DeleteDC, and you would still need to do the nesting correctly. So it's not perfect either. The only "bulletproof" solutions I see would require a complete wrapping of the DC handle, which I would dislike)

we are here to help each other get through this thing, whatever it is Vonnegut jr.

I think that the two destruction methods for DCs (there are more than two if you start to include DirectDraw/3D/Show and GDI+ but that may be taking things two far) are the best reason for not attempting to wrap DCs in classes.

Were you to attempt to do this however, MFCs approach would probably be a good guide. There are three DC classes (plus some other ones for printing that don't matter in this case). CPaintDC gets its DC by calling BeginPaint and releases it with EndPaint and is constructed with a window handle. CClientDC/CWindowDC use the GetDC/GetDCEx functions and ReleaseDC to release them, and straight CDC (which is the base class, but obviously the derived classes override the destruction) which uses DeleteDC to delete the DC which covers most (but not all - see previous comment about DirectX and GDI+) other cases.

Its really a shame that one of the trickiest things to wrap in C++ is also one of the hardest to avoid. I remember starting SDK programming for real on windows having previously used other systems (most notably BeOS) and being thoroughly confused by these DC things. But you can't write even the most trivial GDI application without encountering them.

I don't think virtual is to high of a cost - after all, the bost::shared_ptr solution comes in even higher (and would have made it a no-go for the machines MFC 1.x was intended for)

However, the "battlefield", as I see it, is not virtual vs. templates, but Copy Construction and Assignment.

Since the MFC approach does not provide these, I am forced to pass around CDC * or CDC &, so the wrapper trades in management of one resource (GDI) for another (C++ object lifetime) which is a beast of it's own, and I lose many advantages of scoping.

Reference counting does not plug all holes (SelectObject, as you pointed out, is the gaping barn door, and the template suggested is more a fix than a solution), but it plugs many more holes than it opens, and reduces the "mental overhead" of using GDI resources.

Both the virtual destructor, and the "custom deleter" achieve one thing: associating the destruction policy with the resource at time of acquisition. However, the virtual-only solution prohibits common functionality (passing as argument or return value) and moves the complexity only up one level in the hierarchy.

I would like to emphasize that I don't see the solution presented here as the only one. It is neither perfect, nor the most simple one. But I consider it well balanced, and surprisingly simple once you have "accepted smart pointers in your life".

we are here to help each other get through this thing, whatever it is Vonnegut jr.

I haven't had time to try this, bit I thought you might be able to replace the bool delete on release flag with a template argument and use specialisation to resolve it. Sure it would make the code bigger (but who cares, memory is cheap and hopefully all this stuff gets inlined anyway) but typically you would never pass a non-const value into that field. The type of delete should almost always be known at compile time. And writing a function that can take a dynamic value once you've done it with specialisation is easy. That way the extra cost (small until you start allocation alot of pens with it, or trying to walk thousands of registry keys) is only there for those that need it.

you mean CHandleRef<HANDLE, bool> ? I don't think that's a good idea, since all functions taking such a HandleRef would have to be templates, too. (Or am I missing something? your Stewart Tootill wrote: And writing a function that can take a dynamic value once you've done it with specialisation is easy sounds like I do...)

we are here to help each other get through this thing, whatever it is Vonnegut jr.

Then use CGdiSmartPtr when creating them but functions that only need references, so don't need the concrete type, can accept a reference to the first abstract class, and don't need to be templated. Its probably a lot of effore though

I guess this is partly a question of taste (antd tools you know inside out). I am still convinced MFC has to many drawbacks, and even auto_ptr cannot save that:

auto_ptr does not share, but transfer ownership. I still have to figure out who gets it last.

auto_ptr cannot handle temporary objects, or pointers-to-member

The interface provided by MFC is not simpler than the raw API interface, it requires me to remember more things (That's it's main failure). It works great in an OnPaint function, but beyond it's just silly. Dragging around the deletion policy in a separate variableis silly. Detaching and reattaching the HFONT in a CFont-Member-Setter is silly. (And all functions of MFC expecting a CGdiObject * instead of a HGDIOBJECT is silly, too. But that's another topic completely).

If this sounds like a rant against your favorite method, I am sorry. I am currently in the mood

we are here to help each other get through this thing, whatever it is Vonnegut jr.

1. When sharing HANDLE, only 1 auto pointer should hold the resource and expose (share?) via method or attribute, this auto pointer will release the HANDLE gradually. Is it necessary to known exactly which object holds it? Maybe.

2. You can easily extend auto_ptr (instead of create new class) or simply use auto_ptr_ref to support temporary etc. And in ATL 7 above, there is a class call CAutoPtr.

3. Usually in MFC we try to "remember" (instead of understand?) which class, which method and when. While API you do the same but a "series of APIs". MFC does this for as it wrap "series of APIs" into one call. Example is when main frame resize, how size is calculated amongs various sub-controls.

4. You may stick to MFC default Attach/Detach, or you can extend it with one call, even automatic call. Remember MFC provide a common ground, not a silver bullet. There is no one framework suit all.

Of course, nothing stop you from going deep down to API, or simply WTL.

2) didn't know about auto_ptr_ref, thanks for pointing out!However (you knew that was coming ), it only tries to fix what is already broken: auto_ptr, compared to a refcounted smart pointer, trades of some performance win for quite some additional complexity. I think itwas a bad move to include auto_ptr as the only smart pointer in the STL. If at all, it saved "them" a few tricky design decisions (thread safety? where to store the ref count?)

3) TW wrote:Usually in MFC we try to "remember" (instead of understand?) which class, which method and when.This perfectly describes my main "issue" with MFC, and nowhere this is standing out clearer than with the MFC resource handles (Embedded COM object handling, maybe)

Classes should wrap concepts, not series of commands. Classes should not be silver bullets killing one kind of problem perfectly if shot at the right angle. Rather, classes must be 1) unbreakable in almost every situation and 2) simpler to use than the things they wrap. A kind of Kalashnikow: sturdy, reliable, and usable by everybody who read "programming for dummies".

4) That's basically what I am doing when writing MFC code: use API calls, and using tight attach/detach. Horrible for an MFCian, but it allows me to live with one API intrically known, not two.

The motivation for this article being: MFC did what was appropriate at it's time, for it's purpose. But we can do better, and with the tools available, in much less lines of code than MFC does.

Enjoy

we are here to help each other get through this thing, whatever it is Vonnegut jr.

Talking about resource intensive application, VISIO is certainly one of its kind. Guess what, MFC is used heavily in this VISIO product, you can use Spy++ to check it out.

I won't say MFC is perfect, but I think till today, it is still the best C++ framework available on Windows. It's really up to you how much you want to extend it. We can't keep complaining about MFC "as-is" but does not extend it, right? You can also check out Stingray Objective Studio to see how far MFC can go, just don't be surprise.

You should try the QT framework from Throlltech, they gave me a productivity boost over MFC, ATL etc. etc. and they are portable to Mac and linux!!!!!I have found MFC's one of the worst frameworks around.

I have to disagree with this. MFC was obviously flawed at birth. They took the Model-View-Controller scheme from Smalltalk and broke it by combining the model and controller. Consequently, unless you're very disciplined, the View and Document classes end up as a monolithic disaster, where very little of the code is reusable because the model and UI are all mixed together. This is my main gripe with MFC - it encourages appallingly bad program design.

> I think it was a bad move to include auto_ptr as the only smart pointer in the STL.

I think *everyone* on the Standards committee agrees with that. I heard that the smart pointers were stuffed up because they just ran out time. (Likewise the omission of copy_if). Note that shared_ptr was the first thing admitted into C++0x.

Don Clugston wrote:I think *everyone* on the Standards committee agrees with that.heh! Me agreeing with the Standards commitee on STL

MFC: The first thing I did was drop their Doc/View-Model, (or simply work around it) and do the stuff "my way". I was never sure if this was "just me" or a general flaw. For the rest (i.e. trying to abstract the Windows API) it isn't to bad considering the time it was made, hough a few choices could have been better (and GDI wrapper handles are broken IMO)

we are here to help each other get through this thing, whatever it is Vonnegut jr.

TW wrote:You may also use specialize template instead of functor, to have compiler automatically choose which API to call, when closing a handle.

I tried that initially, but the compiler expects an instantiation of the function, and the linker complainsfor a just-header-library. I guess this is just a compiler thing (should it work according to the standard?)

we are here to help each other get through this thing, whatever it is Vonnegut jr.

You are evil, don't you know?The very moment I ponder which project to get back to next, you remind me of all the irks I have with registry wrappers. Such things should be banned

But since you asked...

Your article is one of the two I consider closest to my expectations (sorry, this sounds like a "diva copmpliment", but it's just I'm rarely satisfied, even with my own code). The only place I'd (now) use boost, would be an intrusive_ptr for the PIMPL idiom.

In general, a registry wrapper would have three API layers:

(1) Wrapping the Windows API in type-safe functions using a CHandleRef<HKEY> instead of HKEY. Open, Get, Set, Get vector of values names, childs, etc. Should be very close to the Win API in naming, argument order, etc. However, I'd use a string class for it, and it would be CString for personal preference.

(2) An extende registry key that, like yours, tracks the whole path, and provides simple "navigation". This would also be the layer that allwos using another storage (XML, Property Bag, etc.)I would, however, use a different access rights management: keys are opened with the least REGSAM required, and track which access rights were granted and not. When another REGSAM is desired, it tries to "bump" the key to the combined requirements. This could completely isolate the user from pondering his necessary rights, without sacrificing performance or restricted environments.

(3) Data Binding to Controls (like your RegDDX), and Data Classes that "act" like a registry value. (i.e. you can assign a CString, get a value of certain type, etc.) The data classes should allow to easily build structures containing configuration values.

Only layer (1) would have to be high performance. The others can trade off quite some speed & memory for convenience.

Damn now you got me ticking...

we are here to help each other get through this thing, whatever it is Vonnegut jr.

Very well written, thanks (like your previous article)! I like to read those contributions, which discuss a problem and show the developement of a solution. Especially when it comes to templates, it is often hard to understand the final solution, whereas it is much more instructive to follow the steps evolving from special to general.

I'd like to ask you for one more explaination: How did you get rid of the delete-operator? Where is the delete actually performed, which you removed from the custom-DELETER?

first, check the declaration of the smart pointer type: it has changed from shared_ptr<HANDLE>> (representing a HANDLE *) to shared_ptr<void> (representing a void *)

All the initial versions did allocate a new HANDLE object on the heap, store the handle itself there, and a pointer to that handle in the smart pointer.

The last version stores the handle in the smart pointer itself - as void *. Now this works only because all the handle types I'm concerned about can be stored as void *. But this sacrifice of flexibility has two advantages: better performance, and the custom deleters are simpler to write.

I could have done this step earlier, but I wanted to carry on the flexible solution as far as I could.

Helped?

we are here to help each other get through this thing, whatever it is Vonnegut jr.

I understand that the subject is "boost", but many concepts, here, does not stictly depend on boost. Boost is a huge library that may be not the case to include if you only need shared_ptr (thay can be easilly coded in a 100 lines header), but the way you solved the sharing of handles problem is really good.

I addressed the problem with a more "policy like" approach here[^], but this makes me reconsider certain choices !!

You are absolutely right, using a reference counter isn't new, there are a few articles on CP here (and now, reading your article I remember seen it before ).

A few things came together in writing the article: I've long pondered the problem and knew my requriements for a solution, I needed a "quick fix" a few days ago and could hack it together in minutes using shared_ptr, I found the process of turning it into a solution interesting enough for another article, and I could feed of the success of my previous one. It's not "very boostish", as the public interface of CHandleRef does not expose anything about boost, it could be reimplemented with any other smart pointer imlementation.

So the idea is indeed implementing a known concept with as few code as possisble, the core is less than 30 lines of actual code. It's not as flexible a a policy design (but I have some "issues" with them - see my response above about Loki), but doing the job.

we are here to help each other get through this thing, whatever it is Vonnegut jr.

Good article. I'm wondering how this idea meshes with WTL which also has a 'bShouldDelete' parameter on creation of any of the 'handle' classes. I think your code could work really well as a WTL extension.

> Second, we can make both the Handle type and the Deleter a template parameter. it's simple for the Handle, but we can't specify a function as template argument.

FYI: Actually you can. Function pointers are permissible as template arguments. It's a bit bizarre, but it's in the standard. It won't work on all compilers (eg, VC6, DMC) and functors are far more useful anyway.

I don't think WTL would like the dependency on boost. However, an "#ifdef boost (use this stuff too)" would certainly be possible.

I consider the concept (using a reference-counted entity with a custom deleter) the minimum for wrapping handles. IIRC the CSmartHandle class (here on CP, mentionend also in a post below) does that with it's own code base, so it might be easier to integrate.