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

8

17

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.

Algebra answered 12/3, 2010 at 14:11 Comment(0)
A
12

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.

Aurelea answered 12/3, 2010 at 14:24 Comment(4)
The code is bad since the struct can be copied. Look at en.wikipedia.org/wiki/Resource_Acquisition_Is_InitializationJorrie
@Kerido, maybe, maybe not. It depends on the semantics of the resource you're wrapping. I think it's fair to give jalf the benefit of the doubt and assume the posted code is just a simple illustrative example.Arneson
@Kerido: So... add two lines making the copy constructor and assignment operator private and undefined?Unknowable
Yes, copying should definitely be prevented if you want a robust solution. I left it out to show a short and simple implementation (which will suffice if you don't try to get clever and copy it). An easy way to prevent copying is to inherit from boost::noncopyable, but yes, otherewise make copy ctor and assignment operator private. But as Kristo said, this was just intended to be illustrative. I intentionally left out the copy constructor for brevity.Aurelea
J
3

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.

Jorrie answered 12/3, 2010 at 14:16 Comment(4)
I only used file handles as a common easy-to-understand example. In practice things are...weirder.Algebra
Which handles did you mean then?Jorrie
SSPI handles like CredHandle, CtxtHandle and SecBufferDesc. The last one is a weird struct which contains a dynamically allocated array of structs where each struct has a pointer to a dynamically allocated buffer. In a nutshell, it's a variable sized collection of variable sized buffers. The releasing function isn't as trivial as just "delete". :(Algebra
Just found this: drdobbs.com/cpp/184401688. Unfortunately I didn't use SSPI so I don't know if the material is appropriate for your case.Jorrie
D
3

These wrappers are called ATL.

If your handle is an event or similar, use CHandle class.

If your handle is a file, use CAtlFile derived one, it wraps APIs like CreateFile and ReadFile.

There’re other useful wrappers in ATL, CAtlFileMapping<T> is a RAII wrapper over memory mapped files, CPath wraps shell32 APIs for path handling, and so on.

ATL is large library, but low-level things like files, strings and collections are isolated. You can use them in all Win32 apps. is header only, you don’t need to link with anything, or distribute extra DLLs like MFC or CRT, the code compiles into WinAPI calls and just works.

They were split from MFC in VS2003 or 2005, don’t remember, i.e. Visual Studio 2008 definitely has them. There’s one caveat however, if you’re using a freeware version of VS, it must be 2015 or newer.

Diphyodont answered 28/6, 2019 at 16:35 Comment(3)
Oooh, going for the Necromancer badge? ;) Good answer though, have my upvote. I don't even remember why I asked this though. :DAlgebra
@Algebra I already have 3 silver ones. BTW, I was searching for FILE* wrappers from <stdio.h> when I found this (I don’t like <iostream>)Diphyodont
I've never really done serious C++ work and the more years have passed, the more I have realized how strange the language has grown. When I look at examples of today's C++ code, most of the time I can't make heads or tails out of it anymore.Algebra
P
1

Here's one based off the EnsureCleanup code from 'Windows via C/C++': http://www.codeproject.com/KB/cpp/template2003.aspx

Pirri answered 12/3, 2010 at 14:34 Comment(0)
K
0

MFC has some suitable primitives (look at CFile for example), but not the standard library.

Kulak answered 12/3, 2010 at 14:13 Comment(5)
Such a class does not sound very complex. Perhaps there is an example implementation on the web somewhere that I could copy-paste in my solution? What keywords should I use in Google for that?Algebra
Look at this for example: bbdsoft.com/win32.html First match for "CreateFile CloseHandle wrapper" query.Kulak
Also CFile and the like will simplify things greatly compared to writing all the code with raw Win32.Kulak
Nice, but I only used file handles as a common easy-to-understand example. In reality I'm dealing with SSPI and handles that need special closing functions and triple-dynamically-allocated-indirect structs. Rare stuff.Algebra
Then user jalf is right on the money. Right your own set of classes - it will take an hour.Kulak
B
0

Visual C++ 2008 supports TR1 through the Feature Pack, and TR1 includes shared_ptr. I would use this -- it's a very powerful smart pointer class and can be generalized to do the type of resource management you are asking for.

TR1 is effectively an extension to the Standard. I believe it's still officially "pre-standard", but effectively you can consider it locked down.

Bombay answered 12/3, 2010 at 14:21 Comment(2)
Note that using shared_ptr for this requires you to write a custom deleter function in some cases. (In simple cases you could just pass eg the CloseHandle function as the deleter.)Sanford
@Sanford - only problem is (I think) you need to check for NULL before calling ::CloseHandle(...), so I think you're stuck with passing a lambdaObovoid
R
0

I don't think there is anything in the standard library, and I also doubt that shared pointers (as in boost) can be used (since those would expect pointer to HANDLE, not HANDLE).

It shouldn't be hard to write one yourself, following the scope guard idiom (and making use of templates/function pointers etc if you so choose).

Ronnironnica answered 12/3, 2010 at 14:40 Comment(0)
C
0
template <typename Traits>
class unique_handle
{
    using pointer = typename Traits::pointer;

    pointer m_value;

    auto close() throw() -> void
    {
        if (*this)
        {
            Traits::close(m_value);
        }
    }

public:

    unique_handle(unique_handle const &) = delete;
    auto operator=(unique_handle const &)->unique_handle & = delete;

    explicit unique_handle(pointer value = Traits::invalid()) throw() :
        m_value{ value }
    {
    }

    unique_handle(unique_handle && other) throw() :
        m_value{ other.release() }
    {
    }

    auto operator=(unique_handle && other) throw() -> unique_handle &
    {
        if (this != &other)
        {
            reset(other.release());
        }

        return *this;
    }

    ~unique_handle() throw()
    {
        close();
    }

    explicit operator bool() const throw()
    {
        return m_value != Traits::invalid();
    }

    auto get() const throw() -> pointer
    {
        return m_value;
    }

    auto get_address_of() throw() -> pointer *
    {
        ASSERT(!*this);
        return &m_value;
    }

    auto release() throw() -> pointer
    {
        auto value = m_value;
        m_value = Traits::invalid();
        return value;
    }

    auto reset(pointer value = Traits::invalid()) throw() -> bool
    {
        if (m_value != value)
        {
            close();
            m_value = value;
        }

        return static_cast<bool>(*this);
    }

    auto swap(unique_handle<Traits> & other) throw() -> void
    {
        std::swap(m_value, other.m_value);
    }
};

template <typename Traits>
auto swap(unique_handle<Traits> & left,
    unique_handle<Traits> & right) throw() -> void
{
    left.swap(right);
}

template <typename Traits>
auto operator==(unique_handle<Traits> const & left,
    unique_handle<Traits> const & right) throw() -> bool
{
    return left.get() == right.get();
}

template <typename Traits>
auto operator!=(unique_handle<Traits> const & left,
    unique_handle<Traits> const & right) throw() -> bool
{
    return left.get() != right.get();
}

template <typename Traits>
auto operator<(unique_handle<Traits> const & left,
    unique_handle<Traits> const & right) throw() -> bool
{
    return left.get() < right.get();
}

template <typename Traits>
auto operator>=(unique_handle<Traits> const & left,
    unique_handle<Traits> const & right) throw() -> bool
{
    return left.get() >= right.get();
}

template <typename Traits>
auto operator>(unique_handle<Traits> const & left,
    unique_handle<Traits> const & right) throw() -> bool
{
    return left.get() > right.get();
}

template <typename Traits>
auto operator<=(unique_handle<Traits> const & left,
    unique_handle<Traits> const & right) throw() -> bool
{
    return left.get() <= right.get();
}

struct null_handle_traits
{
    using pointer = HANDLE;

    static auto invalid() throw() -> pointer
    {
        return nullptr;
    }

    static auto close(pointer value) throw() -> void
    {
        VERIFY(CloseHandle(value));
    }
};

struct invalid_handle_traits
{
    using pointer = HANDLE;

    static auto invalid() throw() -> pointer
    {
        return INVALID_HANDLE_VALUE;
    }

    static auto close(pointer value) throw() -> void
    {
        VERIFY(CloseHandle(value));
    }
};

using null_handle = unique_handle<null_handle_traits>;
using invalid_handle = unique_handle<invalid_handle_traits>;
Colan answered 10/5, 2016 at 11:9 Comment(1)
It's best to add some description to your answer.Tyrannize

© 2022 - 2024 — McMap. All rights reserved.