Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > shared_ptr + this

Reply
Thread Tools

shared_ptr + this

 
 
ralpe
Guest
Posts: n/a
 
      08-23-2004
Hi,

I have a question regarding boost::shared_ptr. I want to let
shared_ptr take care of my objects' lifetimes, so I change
all my function signatures so that they accept shared_ptrs
instead of raw pointers.

I wonder how to deal with cases where a this pointer is
passed as an argument. Is it ok to write something like the
following?

void foo::bar()
{
add_to_collection(boost::shared_ptr<foo>(this));
}

int main()
{
boost::shared_ptr<foo> f(new foo());
foo->bar();
}

I'm asking because shared_ptr is non-intrusive and I fear
that the above code could create a second counter for an
object that is already reference counted.

Any comments?
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-23-2004
ralpe wrote:
....
> I'm asking because shared_ptr is non-intrusive and I fear
> that the above code could create a second counter for an
> object that is already reference counted.
>
> Any comments?


Use intrusive pointers.

 
Reply With Quote
 
 
 
 
Richard Herring
Guest
Posts: n/a
 
      08-23-2004
In message <(E-Mail Removed) >, ralpe
<(E-Mail Removed)> writes
>Hi,
>
>I have a question regarding boost::shared_ptr. I want to let
>shared_ptr take care of my objects' lifetimes, so I change
>all my function signatures so that they accept shared_ptrs
>instead of raw pointers.
>
>I wonder how to deal with cases where a this pointer is
>passed as an argument. Is it ok to write something like the
>following?
>
> void foo::bar()
> {
> add_to_collection(boost::shared_ptr<foo>(this));
> }
>
> int main()
> {
> boost::shared_ptr<foo> f(new foo());
> foo->bar();
> }
>
>I'm asking because shared_ptr is non-intrusive and I fear
>that the above code could create a second counter for an
>object that is already reference counted.
>
>Any comments?


You could have a boost::weak_pointer member initialised from this, and
then use boost::make_shared to create a shared_ptr from it to pass to
add_to_collection. Using the weak_ptr will ensure that all shared_ptrs
created from it will share the same reference count.
--
Richard Herring
 
Reply With Quote
 
ralpe
Guest
Posts: n/a
 
      08-23-2004
Gianni Mariani <(E-Mail Removed)> wrote in message news:<cgc3pr$(E-Mail Removed)>...
> ralpe wrote:
> ...
> > I'm asking because shared_ptr is non-intrusive and I fear
> > that the above code could create a second counter for an
> > object that is already reference counted.
> >
> > Any comments?

>
> Use intrusive pointers.


I chose shared_ptr because it is going to be standardized.
Will there be an intrusive pointer in the next standard?
 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      08-23-2004
On 22 Aug 2004 22:58:55 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (ralpe) wrote:

>Hi,
>
>I have a question regarding boost::shared_ptr. I want to let
>shared_ptr take care of my objects' lifetimes, so I change
>all my function signatures so that they accept shared_ptrs
>instead of raw pointers.
>
>I wonder how to deal with cases where a this pointer is
>passed as an argument. Is it ok to write something like the
>following?
>
> void foo::bar()
> {
> add_to_collection(boost::shared_ptr<foo>(this));
> }
>
> int main()
> {
> boost::shared_ptr<foo> f(new foo());
> foo->bar();
> }
>
>I'm asking because shared_ptr is non-intrusive and I fear
>that the above code could create a second counter for an
>object that is already reference counted.
>
>Any comments?


http://www.boost.org/libs/smart_ptr/...html#from_this

Tom
 
Reply With Quote
 
ralpe
Guest
Posts: n/a
 
      08-23-2004
Richard Herring <junk@[127.0.0.1]> wrote in message news:<(E-Mail Removed)>...
> In message <(E-Mail Removed) >, ralpe
> <(E-Mail Removed)> writes
> >Hi,
> >
> >I have a question regarding boost::shared_ptr. I want to let
> >shared_ptr take care of my objects' lifetimes, so I change
> >all my function signatures so that they accept shared_ptrs
> >instead of raw pointers.
> >
> >I wonder how to deal with cases where a this pointer is
> >passed as an argument. Is it ok to write something like the
> >following?
> >
> > void foo::bar()
> > {
> > add_to_collection(boost::shared_ptr<foo>(this));
> > }
> >
> > int main()
> > {
> > boost::shared_ptr<foo> f(new foo());
> > foo->bar();
> > }
> >
> >I'm asking because shared_ptr is non-intrusive and I fear
> >that the above code could create a second counter for an
> >object that is already reference counted.
> >
> >Any comments?

>
> You could have a boost::weak_pointer member initialised from this, and
> then use boost::make_shared to create a shared_ptr from it to pass to
> add_to_collection. Using the weak_ptr will ensure that all shared_ptrs
> created from it will share the same reference count.


Good idea. Thank you.

Will weak_ptr be part of the next standard?
I only read about shared_ptr.
 
Reply With Quote
 
Simon Turner
Guest
Posts: n/a
 
      08-23-2004
Richard Herring <junk@[127.0.0.1]> wrote:

> In message <(E-Mail Removed) >, ralpe
> <(E-Mail Removed)> writes
> >Hi,
> >
> >I have a question regarding boost::shared_ptr. I want to let
> >shared_ptr take care of my objects' lifetimes, so I change
> >all my function signatures so that they accept shared_ptrs
> >instead of raw pointers.
> >
> >I wonder how to deal with cases where a this pointer is
> >passed as an argument. Is it ok to write something like the
> >following?
> >


<snip>

> >
> >I'm asking because shared_ptr is non-intrusive and I fear
> >that the above code could create a second counter for an
> >object that is already reference counted.
> >
> >Any comments?


It can indeed.

>
> You could have a boost::weak_pointer member initialised from this, and
> then use boost::make_shared to create a shared_ptr from it to pass to
> add_to_collection. Using the weak_ptr will ensure that all shared_ptrs
> created from it will share the same reference count.


This can be automated by deriving your classes from boost::enable_shared_from_this.

See:
http://www.boost.org/libs/smart_ptr/...from_this.html
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      08-23-2004
In message <(E-Mail Removed)>, Simon Turner
<(E-Mail Removed)> writes
>Richard Herring <junk@[127.0.0.1]> wrote:
>
>> In message <(E-Mail Removed) >, ralpe
>> <(E-Mail Removed)> writes
>> >Hi,
>> >
>> >I have a question regarding boost::shared_ptr. I want to let
>> >shared_ptr take care of my objects' lifetimes, so I change
>> >all my function signatures so that they accept shared_ptrs
>> >instead of raw pointers.
>> >
>> >I wonder how to deal with cases where a this pointer is
>> >passed as an argument. Is it ok to write something like the
>> >following?
>> >

>
><snip>
>
>> >
>> >I'm asking because shared_ptr is non-intrusive and I fear
>> >that the above code could create a second counter for an
>> >object that is already reference counted.
>> >
>> >Any comments?

>
>It can indeed.
>
>>
>> You could have a boost::weak_pointer member initialised from this, and
>> then use boost::make_shared to create a shared_ptr from it to pass to
>> add_to_collection. Using the weak_ptr will ensure that all shared_ptrs
>> created from it will share the same reference count.

>
>This can be automated by deriving your classes from
>boost::enable_shared_from_this.
>
>See:
> http://www.boost.org/libs/smart_ptr/...from_this.html



Better still! (enable_... is about two versions of Boost more recent
than my compiler can cope with, so I haven't yet been able to use it
myself )

--
Richard Herring
 
Reply With Quote
 
Marc Schellens
Guest
Posts: n/a
 
      08-24-2004
I know that file functions (see below) are non-standard,
but there must be a 'sub'standard as this are very common
things.

I want to:
get a list of files in a directory,
get the size and access mode of a file, etc.

I am using g++

Thanks,
marc

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
#include <boost/shared_ptr.hpp> or #include "boost/shared_ptr.hpp"? Colin Caughie C++ 1 08-29-2006 02:19 PM
boost::shared_ptr and operator->() Derek C++ 2 12-08-2003 05:13 PM
boost::shared_ptr inside stl::list James Mastro C++ 1 11-13-2003 01:59 AM
error with boost::shared_ptr<T> with incomplete T, VC7.1 ? Philippe Guglielmetti C++ 4 10-09-2003 05:30 PM
shared_ptr vs std::auto_ptr SerGioGio C++ 3 07-03-2003 02:52 PM



Advertisments