Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What wrapper class in C++ should I use for automated resource management?

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.

like image 208
Vilx- Avatar asked Mar 12 '10 14:03

Vilx-


Video Answer


2 Answers

Write your own. It's only a few lines of code. It's just such a simple task that it's not worth it to provide a generic reusable version.

struct FileWrapper {
  FileWrapper(...) : h(CreateFile(...)) {}
  ~FileWrapper() { CloseHandle(h); }

private:
  HANDLE h;
};

Think about what a generic version would have to do: It'd have to be parametrizable so you can specify any pair of functions, and any number of arguments to them. Just instantiating such an object would likely take as many lines of code as the above class definition.

Of course, C++0x might tip the balance somewhat with the addition of lambda expressions. Two lambda expressions could easily be passed to a generic wrapper class, so once C++0x supports comes around, we might see such a generic RAII class added to Boost or something.

But at the moment, it's easier to just roll your own whenever you need it.

As for adding reference counting, I'd advise against it. Reference counting is expensive (suddenly your handle has to be dynamically allocated, and reference counters have to be maintained on every assignment), and very hard to get right. It's an area just bursting with subtle race conditions in a threaded environment.

If you do need reference counting, just do something like boost::shared_ptr<FileWrapper>: wrap your custom ad-hoc RAII classes in a shared_ptr.

like image 159
jalf Avatar answered Oct 02 '22 23:10

jalf


Essentially, fstream is a good C++ wrapper for file handles. It's part of the standard which means it is portable, well tested, and extensible in an object-oriented manner. For file resources, it is a great concept.

However, fstream only works for files, not for generic handles, i.e. threads, processes, synchronization objects, memory-mapped files, etc.

like image 36
Kerido Avatar answered Oct 03 '22 00:10

Kerido