Re: [Boost-users] why doesn't scoped_ptr have a custom
If you intention is to release the pointer, reset(0) can achieve this. The document says: "void reset(T * p = 0); // never throws Deletes the object pointed to by the stored pointer and then stores a copy of p ". With reset, you should be able assign another pointer.
Thanks for your reply...
You're right, "reset" would delete the pointer... but when you're getting
rid of a pointer obtained from a factory, like I was in my case, pointers
don't always get disposed of using delete, but rather another function (like
p->release())
And rather than writing special purpose smart pointers for each type of
deleting function (p->dispose(), p->release(), api_function_free(p), etc..)
you can use shared_ptr with a custom deleter (here:
mem_fn(&pointer_type::dispose) or bind(&api_function_free, _1))
"tom gee"
If you intention is to release the pointer, reset(0) can achieve this. The document says: "void reset(T * p = 0); // never throws Deletes the object pointed to by the stored pointer and then stores a copy of p ".
With reset, you should be able assign another pointer.
Pablo Aguilar wrote:
Thanks for your reply...
You're right, "reset" would delete the pointer... but when you're getting rid of a pointer obtained from a factory, like I was in my case, pointers don't always get disposed of using delete, but rather another function (like p->release())
And rather than writing special purpose smart pointers for each type of deleting function (p->dispose(), p->release(), api_function_free(p), etc..) you can use shared_ptr with a custom deleter (here: mem_fn(&pointer_type::dispose) or bind(&api_function_free, _1))
In some cases you can avoid the space and time overhead of shared_ptr by using a smart pointer template for which the custom deleter is a template argument. In fact you can get even more general than that quite easily; I have a class template called auto_handle which with minimal work can be used to encapsulate all kinds of things with custom deleters, including Windows handles and POSIX file descriptors. Ben.
Good point, I just didn't bother writing a handle class given I knew I could
use shared_ptr and the space/time overhead wasn't important.
Anyway, I'll take a shot at writing an auto_handle class.
"Ben Hutchings"
Pablo Aguilar wrote:
Thanks for your reply...
You're right, "reset" would delete the pointer... but when you're getting rid of a pointer obtained from a factory, like I was in my case, pointers don't always get disposed of using delete, but rather another function (like p->release())
And rather than writing special purpose smart pointers for each type of deleting function (p->dispose(), p->release(), api_function_free(p), etc..) you can use shared_ptr with a custom deleter (here: mem_fn(&pointer_type::dispose) or bind(&api_function_free, _1))
In some cases you can avoid the space and time overhead of shared_ptr by using a smart pointer template for which the custom deleter is a template argument. In fact you can get even more general than that quite easily; I have a class template called auto_handle which with minimal work can be used to encapsulate all kinds of things with custom deleters, including Windows handles and POSIX file descriptors.
Ben.
Ben Hutchings wrote:
In some cases you can avoid the space and time overhead of shared_ptr by using a smart pointer template for which the custom deleter is a template argument.
This is what static_move_ptr does: http://tinyurl.com/52zk4.
Ben.
Jonathan
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Ben Hutchings Sent: Thursday, January 06, 2005 6:25 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] Re: why doesn't scoped_ptr have a custom
Pablo Aguilar wrote:
Thanks for your reply...
You're right, "reset" would delete the pointer... but when you're getting rid of a pointer obtained from a factory, like I was in my case, pointers don't always get disposed of using delete, but rather another function (like p->release())
And rather than writing special purpose smart pointers for each type of deleting function (p->dispose(), p->release(), api_function_free(p), etc..) you can use shared_ptr with a custom deleter (here: mem_fn(&pointer_type::dispose) or bind(&api_function_free, _1))
In some cases you can avoid the space and time overhead of shared_ptr by using a smart pointer template for which the custom deleter is a template argument. In fact you can get even more general than that quite easily; I have a class template called auto_handle which with minimal work can be used to encapsulate all kinds of things with custom deleters, including Windows handles and POSIX file descriptors.
Good point. Probably most developers in this group have done something like that; mine is an editor macro customized for my favorite editor, I use it so often. It lets me supply the parameterizations using an OS-specific, but minimally-intrusive interface as I edit. Reid
participants (5)
-
Ben Hutchings
-
Jonathan Turkanis
-
Pablo Aguilar
-
Reid Sweatman
-
tom gee