shared_ptr / auto_ptr

Hi, is there any reason why shared_ptr does not have a ctor that takes an auto_ptr and a deleter? I also wonder if there is a resource leak if I just write: auto_ptr<T> a = ...; my_deleter d = ...; shared_ptr<T> s( a.release(), d ); Regards, Daniel

Daniel Frey:
Hi,
is there any reason why shared_ptr does not have a ctor that takes an auto_ptr and a deleter?
shared_ptr assumes that an object that is owned by an auto_ptr needs to be destroyed by delete, because that is what the auto_ptr would do if, for example, an exception is thrown before the line that transfers the ownership to a shared_ptr.
I also wonder if there is a resource leak if I just write:
auto_ptr<T> a = ...; my_deleter d = ...;
shared_ptr<T> s( a.release(), d );
No, there isn't.

On Fri, 2008-03-21 at 16:30 +0200, Peter Dimov wrote:
Daniel Frey:
is there any reason why shared_ptr does not have a ctor that takes an auto_ptr and a deleter?
shared_ptr assumes that an object that is owned by an auto_ptr needs to be destroyed by delete, because that is what the auto_ptr would do if, for example, an exception is thrown before the line that transfers the ownership to a shared_ptr.
In my scenario, the deleter tries to store the object in a pool instead of deleting it. From that pool, it will later be either reused or actually deleted. Since I'm just deferring the 'delete', it's fine when auto_ptr does it immediately in case of exceptions.
I also wonder if there is a resource leak if I just write:
auto_ptr<T> a = ...; my_deleter d = ...;
shared_ptr<T> s( a.release(), d );
No, there isn't.
Good to know. As long as this works, the missing ctor is no big deal for me. Thanks. Regards, Daniel
participants (2)
-
Daniel Frey
-
Peter Dimov