boost shared_ptr with own dtor

Hello, I'm using boost::shared_ptr for using a C pointer. The ctor and all functions are called without any problem, but I have got a problem with the dtor. I need to call a C function on the dtor eg cptr_close( mypointer ) How can I this used with boost::shared_ptr? I need the cptr_close call on the pointer member of the dtor on the shared_ptr. Thanks a lot Phil

Philipp Kraus
Hello,
I'm using boost::shared_ptr for using a C pointer. The ctor and all functions are called without any problem, but I have got a problem with the dtor. I need to call a C function on the dtor eg
cptr_close( mypointer )
How can I this used with boost::shared_ptr? I need the cptr_close call on the pointer member of the dtor on the shared_ptr.
Thanks a lot
Phil
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I would initialize the shared pointer like so: boost::shared_ptr <MyType> ptr; MyType *temp_ptr = cptr_open(); //Use a temporary pointer to avoid initializing the shared pointer with null. if (temp_ptr) ptr.reset(temp_ptr, &cptr_close); //Now discard temp_ptr

On 2012-12-08 17:45:07 +0100, Andrew Holden said:
Philipp Kraus
wrote: Hello, I'm using boost::shared_ptr for using a C pointer. The ctor and all functions are called without any problem, but I have got a problem with the dtor. I need to call a C function on the dtor eg
cptr_close( mypointer )
How can I this used with boost::shared_ptr? I need the cptr_close call on the pointer member of the dtor on the shared_ptr.
Thanks a lot
Phil
Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I would ini tialize the shared pointer like so:
boost::shared_ptr <MyType> ptr;
MyType *temp_ptr = cptr_open(); //Use a temporary pointer to avoid initializing the shared pointer with null.
if (temp_ptr) ptr.reset(temp_ptr, &cptr_close); //Now discard temp_ptr
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
O
http://www.boost.org/doc/libs/1_52_0/libs/smart_ptr/shared_ptr.htm#construct...
I found that I can use a ctor with
template

On Sat, Dec 08, 2012 at 06:24:08PM +0100, Philipp Kraus wrote:
On 2012-12-08 17:45:07 +0100, Andrew Holden said:
I would ini tialize the shared pointer like so:
boost::shared_ptr <MyType> ptr;
MyType *temp_ptr = cptr_open(); //Use a temporary pointer to avoid initializing the shared pointer with null.
if (temp_ptr) ptr.reset(temp_ptr, &cptr_close); //Now discard temp_ptr
O http://www.boost.org/doc/libs/1_52_0/libs/smart_ptr/shared_ptr.htm#construct... I found that I can use a ctor with template
shared_ptr(Y * p, D d); and D is a dtor call. Do you have an example for D ?
'D' can be any type whose value 'd' is callable as d(p). This includes,
but is not limited to:
D: std::function

Am 08.12.2012 um 18:43 schrieb Lars Viklund:
On Sat, Dec 08, 2012 at 06:24:08PM +0100, Philipp Kraus wrote:
On 2012-12-08 17:45:07 +0100, Andrew Holden said:
I would ini tialize the shared pointer like so:
boost::shared_ptr <MyType> ptr;
MyType *temp_ptr = cptr_open(); //Use a temporary pointer to avoid initializing the shared pointer with null.
if (temp_ptr) ptr.reset(temp_ptr, &cptr_close); //Now discard temp_ptr
O http://www.boost.org/doc/libs/1_52_0/libs/smart_ptr/shared_ptr.htm#construct... I found that I can use a ctor with template
shared_ptr(Y * p, D d); and D is a dtor call. Do you have an example for D ? 'D' can be any type whose value 'd' is callable as d(p). This includes, but is not limited to: D: std::function
// friendly wrapper around callables D: void (*)(Y*) // regular function pointer to say void f(Y* p); D: struct D { void operator () (Y* p) const; }; d: [](Y* p) { .. } // lambdas are neat In your case, Andrew outlined how you can pass in a function pointer to your cptr_close, assuming that it has a suitable signature.
The D used in reset() adheres to the same rules as the one in the constructor your found, namely that it'll be invoked when the last sibling shared_ptr perishes.
I'm using the ctor call boost::shared_ptr<mycptr> x ( cptr_open(), cptr_close ); this seems to be working at the moment Thx Phil

Philipp Kraus
Am 08.12.2012 um 18:43 schrieb Lars Viklund:
On Sat, Dec 08, 2012 at 06:24:08PM +0100, Philipp Kraus wrote:
On 2012-12-08 17:45:07 +0100, Andrew Holden said:
I would ini tialize the shared pointer like so:
boost::shared_ptr <MyType> ptr;
MyType *temp_ptr = cptr_open(); //Use a temporary pointer to avoid
initializing the shared pointer with null.
if (temp_ptr) ptr.reset(temp_ptr, &cptr_close); //Now discard temp_ptr
O
http://www.boost.org/doc/libs/1_52_0/libs/smart_ptr/shared_ptr.htm#construct...
I found that I can use a ctor with template
shared_ptr(Y * p, D d); and D is a dtor call. Do you have an example for D ? 'D' can be any type whose value 'd' is callable as d(p). This includes, but is not limited to: D: std::function
// friendly wrapper around callables D: void (*)(Y*) // regular function pointer to say void f(Y* p); D: struct D { void operator () (Y* p) const; }; d: [](Y* p) { .. } // lambdas are neat In your case, Andrew outlined how you can pass in a function pointer to your cptr_close, assuming that it has a suitable signature.
The D used in reset() adheres to the same rules as the one in the constructor your found, namely that it'll be invoked when the last sibling shared_ptr perishes.
I'm using the ctor call boost::shared_ptr<mycptr> x ( cptr_open(), cptr_close );
this seems to be working at the moment
Thx
Phil _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
That will work unless cptr_open() returns NULL. If I recall, if cptr_open() returns NULL, then the shared_ptr will report it is initialized correctly even though it contains a NULL pointer, and will call cptr_close (NULL) when it goes out of scope.

On Sat, Dec 08, 2012 at 01:34:18PM -0500, Andrew Holden wrote:
That will work unless cptr_open() returns NULL. If I recall, if cptr_open() returns NULL, then the shared_ptr will report it is initialized correctly even though it contains a NULL pointer, and will call cptr_close (NULL) when it goes out of scope.
This is of course not necessarily an error if your deleter is documented to Do The Right Thing on nulls, like 'free', as well as simple wrappers around 'delete' and 'delete[]'. Also note that while it's "initialized correctly", operator bool-ish will still evaluate to false for a held nullptr. Implementor beware, as always. -- Lars Viklund | zao@acc.umu.se

On Dec 8, 2012, at 2:18 PM, Lars Viklund
On Sat, Dec 08, 2012 at 01:34:18PM -0500, Andrew Holden wrote:
That will work unless cptr_open() returns NULL. If I recall, if cptr_open() returns NULL, then the shared_ptr will report it is initialized correctly even though it contains a NULL pointer, and will call cptr_close (NULL) when it goes out of scope.
This is of course not necessarily an error if your deleter is documented to Do The Right Thing on nulls, like 'free', as well as simple wrappers around 'delete' and 'delete[]'.
True, and if cptr_close was written by the O.P., then adding a check for NULL would be trivial and would not have a measurable impact on runtime.
Also note that while it's "initialized correctly", operator bool-ish will still evaluate to false for a held nullptr.
Good point. For whatever reason (I can't remember why) I got into the habit of calling shared_ptr::empty() instead of using the pointer as a bool. You just gave me a reason to revisit that decision.
participants (3)
-
Andrew Holden
-
Lars Viklund
-
Philipp Kraus