shared_ptr + this

R

ralpe

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?
 
G

Gianni Mariani

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.
 
R

Richard Herring

ralpe said:
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.
 
R

ralpe

Gianni Mariani said:
ralpe wrote:
...

Use intrusive pointers.

I chose shared_ptr because it is going to be standardized.
Will there be an intrusive pointer in the next standard?
 
T

tom_usenet

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
 
R

ralpe

Richard Herring said:
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.
 
M

Marc Schellens

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top