473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?
Jul 22 '05 #1
8 2148
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.

Jul 22 '05 #2
In message <42**************************@posting.google.com >, ralpe
<ra************@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
Jul 22 '05 #3
On 22 Aug 2004 22:58:55 -0700, ra************@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
Jul 22 '05 #4
Gianni Mariani <gi*******@mariani.ws> wrote in message news:<cg*******@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?
Jul 22 '05 #5
Richard Herring <ju**@[127.0.0.1]> wrote in message news:<VE**************@baesystems.com>...
In message <42**************************@posting.google.com >, ralpe
<ra************@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.
Jul 22 '05 #6
Richard Herring <ju**@[127.0.0.1]> wrote:
In message <42**************************@posting.google.com >, ralpe
<ra************@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
Jul 22 '05 #7
In message <ea************************@posting.google.com>, Simon Turner
<s_********@yahoo.co.uk> writes
Richard Herring <ju**@[127.0.0.1]> wrote:
In message <42**************************@posting.google.com >, ralpe
<ra************@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
Jul 22 '05 #8
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

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include...
7
by: myfavdepo | last post by:
Hi all, I have a query regarding the exchanging of a boost::shared_ptr beween different threads. In my program i've two threads both of which having their own internal queues for storing the...
3
by: Emmanuel Deloget | last post by:
Hello y'all, I'm currently writing a serie of blog tickets about the TR1, both from a definition point of view and from an implementation point of view. The next iteration in the series deals...
3
by: Tim H | last post by:
I'm newish to C++ but not to C. I'm confused by this code. test1() is fine. test2() fails to compile. /tmp/inherit_ptr.cpp: In function âvoid test2()â: /tmp/inherit_ptr.cpp:52: error: no...
14
by: Tim H | last post by:
I understand the semantics of why this works the way it does. But I wonder if there's a reason for the behaviore at the line marked "QUESTION". I figured if there is an answer, someone here knows...
9
by: Tim H | last post by:
Why is the following code not valid? I mean, I see the code and it doesn't allow it, but I am curious about the rationale? boost::shared_ptr<intpi = new int; pi = new int; Thanks Tim
8
by: er | last post by:
Hi All, could anyone make a suggestion to fix the code below? Thanks! class A{ public: /* constructor */ double value()const{/* implementation */}; }; typedef...
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
5
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
5
by: .rhavin grobert | last post by:
are there any pro's and con's in using references to shared_ptr<>'s ? example: __________________________________________________ void obj::foo(shared_ptr<>); vs. void...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.