Connecting Tech Pros Worldwide Help | Site Map

shared_ptr + this

ralpe
Guest
 
Posts: n/a
#1: Jul 22 '05
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?
Gianni Mariani
Guest
 
Posts: n/a
#2: Jul 22 '05

re: shared_ptr + this


ralpe wrote:
....[color=blue]
> 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?[/color]

Use intrusive pointers.

Richard Herring
Guest
 
Posts: n/a
#3: Jul 22 '05

re: shared_ptr + this


In message <426691c0.0408222158.1f00e1fd@posting.google.com >, ralpe
<ralph.peterson@gmx.net> writes[color=blue]
>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?[/color]

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
tom_usenet
Guest
 
Posts: n/a
#4: Jul 22 '05

re: shared_ptr + this


On 22 Aug 2004 22:58:55 -0700, ralph.peterson@gmx.net (ralpe) wrote:
[color=blue]
>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?[/color]

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

Tom
ralpe
Guest
 
Posts: n/a
#5: Jul 22 '05

re: shared_ptr + this


Gianni Mariani <gi2nospam@mariani.ws> wrote in message news:<cgc3pr$6s@dispatch.concentric.net>...[color=blue]
> ralpe wrote:
> ...[color=green]
> > 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?[/color]
>
> Use intrusive pointers.[/color]

I chose shared_ptr because it is going to be standardized.
Will there be an intrusive pointer in the next standard?
ralpe
Guest
 
Posts: n/a
#6: Jul 22 '05

re: shared_ptr + this


Richard Herring <junk@[127.0.0.1]> wrote in message news:<VE2ttlerZcKBFwNx@baesystems.com>...[color=blue]
> In message <426691c0.0408222158.1f00e1fd@posting.google.com >, ralpe
> <ralph.peterson@gmx.net> writes[color=green]
> >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?[/color]
>
> 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.[/color]

Good idea. Thank you.

Will weak_ptr be part of the next standard?
I only read about shared_ptr.
Simon Turner
Guest
 
Posts: n/a
#7: Jul 22 '05

re: shared_ptr + this


Richard Herring <junk@[127.0.0.1]> wrote:
[color=blue]
> In message <426691c0.0408222158.1f00e1fd@posting.google.com >, ralpe
> <ralph.peterson@gmx.net> writes[color=green]
> >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?
> >[/color][/color]

<snip>
[color=blue][color=green]
> >
> >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?[/color][/color]

It can indeed.
[color=blue]
>
> 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.[/color]

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
Richard Herring
Guest
 
Posts: n/a
#8: Jul 22 '05

re: shared_ptr + this


In message <ea3f115.0408230600.e9f9915@posting.google.com>, Simon Turner
<s_j_turner@yahoo.co.uk> writes[color=blue]
>Richard Herring <junk@[127.0.0.1]> wrote:
>[color=green]
>> In message <426691c0.0408222158.1f00e1fd@posting.google.com >, ralpe
>> <ralph.peterson@gmx.net> writes[color=darkred]
>> >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?
>> >[/color][/color]
>
><snip>
>[color=green][color=darkred]
>> >
>> >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?[/color][/color]
>
>It can indeed.
>[color=green]
>>
>> 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.[/color]
>
>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[/color]


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
Marc Schellens
Guest
 
Posts: n/a
#9: Jul 22 '05

re: shared_ptr + this


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

Closed Thread