smart_ptr and handles

Dear group, is it possible to use smart_ptr with handles ( e.g. HWND, HBRUSH, HDC, etc )? Wouldn't it be quite convenient to have something like a scoped or shared resource? The idea came when I looked my OpenGL initialisation code, where I have to free the resource explictly every time an error occurs, which make the code somehow a little bit more difficult to read and I though that it would be nice to have some kind of RAII mechanism here. Regards, -Dirk

Using a handle alone won't work, but if you wrap them in objects (that destroy them on a destructor) and use those in the smart pointers, it should work fine. On Tue, 28 Dec 2004 22:16:05 +0100, Dirk Gregorius <dirk@dirkgregorius.de> wrote:
Dear group,
is it possible to use smart_ptr with handles ( e.g. HWND, HBRUSH, HDC, etc )? Wouldn't it be quite convenient to have something like a scoped or shared resource? The idea came when I looked my OpenGL initialisation code, where I have to free the resource explictly every time an error occurs, which make the code somehow a little bit more difficult to read and I though that it would be nice to have some kind of RAII mechanism here.
Regards,
-Dirk _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
-- Cory Nelson http://www.int64.org

is it possible to use smart_ptr with handles ( e.g. HWND, HBRUSH, HDC, etc )?
Policy based smart pointer easily handle HANDLE ;))
I believe shared_ptr with a custom deleter can work with handles (which are really just pointers in disguise), probably something like: void handle_deleter(void* p) { ::CloseHandle((HANDLE)p); } shared_ptr<void> phandle(my_handle, &handle_deleter); Regards, John.

On Tue, 28 Dec 2004 22:16:05 +0100, Dirk Gregorius <dirk@dirkgregorius.de> wrote:
is it possible to use smart_ptr with handles ( e.g. HWND, HBRUSH, HDC, etc )?
I believe what you want is documented at http://www.boost.org/libs/smart_ptr/sp_techniques.html#handle Regards, Rogier
participants (5)
-
Cory Nelson
-
Dirk Gregorius
-
Gennadiy Rozental
-
John Maddock
-
Rogier van Dalen