Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   shared_ptr + this (http://www.velocityreviews.com/forums/t285167-shared_ptr-this.html)

ralpe 08-23-2004 05:58 AM

shared_ptr + this
 
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 08-23-2004 06:45 AM

Re: shared_ptr + this
 
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.


Richard Herring 08-23-2004 10:26 AM

Re: shared_ptr + this
 
In message <426691c0.0408222158.1f00e1fd@posting.google.com >, ralpe
<ralph.peterson@gmx.net> 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

ralpe 08-23-2004 01:21 PM

Re: shared_ptr + this
 
Gianni Mariani <gi2nospam@mariani.ws> wrote in message news:<cgc3pr$6s@dispatch.concentric.net>...
> 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?

tom_usenet 08-23-2004 01:25 PM

Re: shared_ptr + this
 
On 22 Aug 2004 22:58:55 -0700, ralph.peterson@gmx.net (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

ralpe 08-23-2004 01:28 PM

Re: shared_ptr + this
 
Richard Herring <junk@[127.0.0.1]> wrote in message news:<VE2ttlerZcKBFwNx@baesystems.com>...
> In message <426691c0.0408222158.1f00e1fd@posting.google.com >, ralpe
> <ralph.peterson@gmx.net> 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.

Simon Turner 08-23-2004 02:00 PM

Re: shared_ptr + this
 
Richard Herring <junk@[127.0.0.1]> wrote:

> In message <426691c0.0408222158.1f00e1fd@posting.google.com >, ralpe
> <ralph.peterson@gmx.net> 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

Richard Herring 08-23-2004 02:57 PM

Re: shared_ptr + this
 
In message <ea3f115.0408230600.e9f9915@posting.google.com>, Simon Turner
<s_j_turner@yahoo.co.uk> writes
>Richard Herring <junk@[127.0.0.1]> wrote:
>
>> In message <426691c0.0408222158.1f00e1fd@posting.google.com >, ralpe
>> <ralph.peterson@gmx.net> 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

Marc Schellens 08-24-2004 07:20 AM

enhanced file info
 
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



All times are GMT. The time now is 12:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.