Click here to Skip to main content
Click here to Skip to main content

Scoped Handle - "Smart pointer" for Windows objects

By , 21 Jul 2010
 

Introduction

One of the common problems with Windows object handles to solve is to prevent resource leaks. In other words, it is necessary not to forget to call the appropriate delete handle method.

To call the appropriate delete handle method explicitly "by hand" is annoying and error prone. Moreover, to call the appropriate delete handle method during an exception is impossible. Therefore, some automatic calling of the delete handle method would be very handy.

This article describes the simple universal solution of this problem using the well-known RAII (Resource Acquisition Is Initialization) technique. It introduces a scoped handle class which calls in its destructor the appropriate delete handle method.

This scoped handle class has a very similar implementation to the smart pointer scoped_ptr from the boost C++ library.

Solution

The easy solution is to introduce the scoped handle class which calls in its destructor the delete handle method.

However, the Windows API world is not so straightforward. There is no common delete methods for all handles. For example, handle returned from the CreateEvent() API method needs to be deleted by the CloseHandle() method. But handle returned from the FindFirstFile() API method needs to be deleted by the FindClose() method.

Moreover, even the invalid handle definition is not always the same. For example, handle returned from the CreateEvent() API method is invalid when it is NULL. But handle returned from the CreateFile() API method is invalid when it is INVALID_HANDLE_VALUE.

To avoid many similar scoped handle implementations, universal template solution is introduced:

template <typename HandleType, class InvalidValue, class CloseMethod>
class ScopedHandle
{
};

This template has three parameters:

  • HandleType - The type of the Windows API handle. E.g., HANDLE, SOCKET, HMODULE.
  • InvalidValue - The class which implements a static public method Get() which returns the invalid handle value.
  • CloseMethod - The class which implements a static public method Close() which deletes the given handle.

The reason why the template does not use non-class parameters is to support old C++ compilers as well.

Usage

The following is the definition of the scoped handle class for handle returned from the CreateFile() method:

class InvalidHandleValue
{
  public:
        static inline HANDLE Get() { return INVALID_HANDLE_VALUE; }
};

class CloseHandleMethod
{
  public:
        static inline void Close(HANDLE Handle) { CloseHandle(Handle); }
};

typedef ScopedHandle<HANDLE, InvalidHandleValue, CloseHandleMethod> FileScopedHandle;

The usage is as follows:

FileScopedHandle File( CreateFile(_T("SomeFile.bin"), GENERIC_READ, 0, 
                 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL) );
if (!File)
  throw std::runtime_error("File open failure!");
const DWORD FileSize = GetFileSize(File.Get(), NULL);

Conclusion

This article shows the implementation and usage of a scoped handle class which automatically calls the delete handle method during its destruction.

This class is easy to use, and it is flexible enough for almost all possible object handles. To use this class means to avoid any possible resource leaks, and you will not have to pay any attention to this problem anymore.

Links

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Miki Rozloznik
Software Developer (Senior) Eccam s.r.o.
Czech Republic Czech Republic
Member
Senior Sofware Developer focused on embedded systems.
Company website: http://www.eccam.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 2memberAescleal22 Jul '10 - 2:41 
Can't see the point of using this class when there are things like SCOPEGUARD and unique_ptr around that'll do the same thing.
GeneralRe: My vote of 2memberAndre xxxxxxx22 Jul '10 - 4:57 
Obviously you don't know the difference between a pointer and a kernel handle.
GeneralRe: My vote of 2memberAescleal22 Jul '10 - 5:33 
Au contraire my little downvoter, I'm well aware of how I can use unique_ptr, shared_ptr and ScopeGuard to handle windows handles. Unfortunately I don't do kernel mode programming but the examples used in the article were all user mode so I don't think I'm going to loose much sleep over this.
 
Consider the following (I'm using VC++2008 without SP1 installed as that's the IDE I've got open...)
 
typedef std::tr1::shared_ptr<void> managed_handle;
 
That definition will give you everything the article describes. So I can write things like:
 
managed_handle h( CreateMutex( 0, FALSE, 0 ), CloseHandle );
 
and I get a handle to the user mode image of a kernel object that will be cleaned up when the object is deleted. I can also use it as:
 
WIN32_FIND_DATA fd;
managed_handle h( FindFirstFile( L"c:\\test.bat", &fd ), FindClose );
 
to get round one of the problems that have been posted to the message boards several times in the last three months.
 
So the question remains, what can this class do that others can't? What's it's unique selling point? Why should I be using it instead of ScopeGuard which is 10 years old?
 
Edited as I forgot the whole webby HTML escapy thing for angle brackets
GeneralRe: My vote of 2memberemilio_grv22 Jul '10 - 11:46 
Ash, consider that shared_ptr is costly in term of allocation and memory (it is a two pointers holding, one of which points to a struct holding a pointer, two counters and a function pointer). May be too much for just a handle.
 
Also, the proposed class is completely static, while your construction-based definitions are runtime.
 
In the most of the cases they are equivalently functional, but sometime one may be preferable to the other.
Especially with "old compilers"

2 bugs found.
> recompile ...
65534 bugs found.
D'Oh! | :doh:


GeneralRe: My vote of 2 [modified]memberAescleal22 Jul '10 - 20:11 
As I said, I was just using shared_ptr as that was all I had installed on the machine I replied on. How about Alexandrescu and Martigan's ScopeGuard? It's been around 10 years:
 
ScopeGuard managed_handle( MakeScopeGuard( h, CloseHandle) );
 
It's as efficient as the author's code and has the advantage of being pretty well known.
 
I still don't get what the author's class gives me that others don't. Don't get me wrong it's a neat idea but this sort of thing has been well known in the C++ community for 10 years and I'm going to take convincing this is better.
 
Cheers,
 
Ash
 
PS: Forgot to say thanks for a reasoned reply compared to the post I answered

modified on Friday, July 23, 2010 2:18 AM

GeneralRe: My vote of 2memberMiki Rozloznik22 Jul '10 - 21:47 
Hello Ash,
 
thanks a lot for your reactions.
 
I think ScopedGuard should be equivalent. However, the scoped handle solves the problem with different invalid handle values. So, you can use expressions like if (!File) without paying any attention which value means invalid handle in this case. So, it is less error prone and more convenient to use. So sometimes one may prefer this solution.
 
The idea is so old as C++ language is, so this is not the presentation of new idea, this is a presentation of handy solution for specific objects (Windows object handles).
 
Cheers,
 
Miki
GeneralRe: My vote of 2memberemilio_grv22 Jul '10 - 21:48 
You still have to store the CloseHandle function pointer for later use, hence, it is runtime. (And the function pointer can be even a pointer to a polymorphic object, if you want the close policy to be whatever function or functor)
 
About efficiency, ScopeGuard is surely more efficient then a shared_ptr, where no share is required, and may be even more efficient that the author proposal (that requires to code things around, that take their space and time).
But it is another thing.
 
Note that ScopeGuard is like strcpy: if you don't remember how it is named, it is faster for you to rewrite it than to search for it. That's the reason why there are at least 1000 other implementations around Smile | :)
 
That said, for the cases you are talking about, there is no much difference (you don't have milions of handles, so space is not a problem) and the code is far more clear.

2 bugs found.
> recompile ...
65534 bugs found.
D'Oh! | :doh:


GeneralRe: My vote of 2memberMiki Rozloznik22 Jul '10 - 20:25 
It is very similar like differences between shared_ptr and scoped_ptr. Why we need scoped_ptr as well? Because it is noncopyable which is safer than shared_ptr. Because scoped_ptr is simple, every operation is as fast as for a built-in pointer and it has no more space overhead that a built-in pointer. Because scoped_ptr lets readers of your code know that you intend to use it in the current scope and transfer ownership is not needed.
 
Miki
GeneralRe: My vote of 2memberAescleal22 Jul '10 - 22:40 
If your criteria are:
 
- non-copying
- same size as a builtin
 
then ScopeGuard gave us that 10 years ago and std::unique_ptr gives you that out of the box in C++0x.
 
I think we'll just have to agree to differ on the necessity for your class.
 
Cheers,
 
Ash

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 21 Jul 2010
Article Copyright 2010 by Miki Rozloznik
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid