I'm a C++ amateur. I'm writing some Win32 API code and there are handles and weirdly compositely allocated objects aplenty. So I was wondering - is there some wrapper class that would make resource management easier?
For example, when I want to load some data I open a file with CreateFile()
and get a HANDLE
. When I'm done with it, I should call CloseHandle()
on it. But for any reasonably complex loading function there will be dozens of possible exit points, not to mention exceptions.
So it would be great if I could wrap the handle in some kind of wrapper class which would automatically call CloseHandle()
once execution left the scope. Even better - it could do some reference counting so I can pass it around in and out of other functions, and it would release the resource only when the last reference left scope.
The concept is simple - but is there something like that in the standard library? I'm using Visual Studio 2008, by the way, and I don't want to attach a 3rd party framework like Boost or something.