shared_ptr + this

Discussion in 'C++' started by ralpe, Aug 23, 2004.

  1. ralpe

    ralpe Guest

    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?
    ralpe, Aug 23, 2004
    #1
    1. Advertising

  2. 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.
    Gianni Mariani, Aug 23, 2004
    #2
    1. Advertising

  3. In message <>, ralpe
    <> 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
    Richard Herring, Aug 23, 2004
    #3
  4. ralpe

    ralpe Guest

    Gianni Mariani <> wrote in message news:<cgc3pr$>...
    > 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?
    ralpe, Aug 23, 2004
    #4
  5. ralpe

    tom_usenet Guest

    On 22 Aug 2004 22:58:55 -0700, (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/sp_techniques.html#from_this

    Tom
    tom_usenet, Aug 23, 2004
    #5
  6. ralpe

    ralpe Guest

    Richard Herring <junk@[127.0.0.1]> wrote in message news:<>...
    > In message <>, ralpe
    > <> 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.
    ralpe, Aug 23, 2004
    #6
  7. ralpe

    Simon Turner Guest

    Richard Herring <junk@[127.0.0.1]> wrote:

    > In message <>, ralpe
    > <> 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/enable_shared_from_this.html
    Simon Turner, Aug 23, 2004
    #7
  8. In message <>, Simon Turner
    <> writes
    >Richard Herring <junk@[127.0.0.1]> wrote:
    >
    >> In message <>, ralpe
    >> <> 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/enable_shared_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
    Richard Herring, Aug 23, 2004
    #8
  9. 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
    Marc Schellens, Aug 24, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SerGioGio

    shared_ptr vs std::auto_ptr

    SerGioGio, Jul 3, 2003, in forum: C++
    Replies:
    3
    Views:
    5,434
    Alexander Terekhov
    Jul 3, 2003
  2. Philippe Guglielmetti
    Replies:
    4
    Views:
    885
    tom_usenet
    Oct 9, 2003
  3. James Mastro

    boost::shared_ptr inside stl::list

    James Mastro, Nov 13, 2003, in forum: C++
    Replies:
    1
    Views:
    2,020
    Cy Edmunds
    Nov 13, 2003
  4. Derek
    Replies:
    2
    Views:
    617
    Derek
    Dec 8, 2003
  5. Colin Caughie
    Replies:
    1
    Views:
    693
    Shooting
    Aug 29, 2006
Loading...

Share This Page