Reference Counted Smart Pointers And STL Containers

M

Matthias Kaeppler

Hi,

I was wondering, since STL containers are based around copying, whether
it's a good idea to use reference counted smart pointers, such as
boost::shared_ptr in STL containers.
I can't store the objects directly in a container, because they must
not be duplicated, so I have to use pointers. I'm just not certain about
using raw pointers or some kind of smart pointer.

Regards,
Matthias
 
P

peter.koch.larsen

Matthias Kaeppler skrev:
Hi,

I was wondering, since STL containers are based around copying, whether
it's a good idea to use reference counted smart pointers, such as
boost::shared_ptr in STL containers.
I can't store the objects directly in a container, because they must
not be duplicated, so I have to use pointers. I'm just not certain about
using raw pointers or some kind of smart pointer.

Regards,
Matthias

A reference-counted pointer such as boost::shared_ptr is almost always
the correct choice in situations such as this.

/Peter
 
M

Matthias Kaeppler

A reference-counted pointer such as boost::shared_ptr is almost always
the correct choice in situations such as this.

Hm, yes probably you're right. It's just that I'm getting segfaults on
program exit, and it's almost certainly due to the smart pointers I'm
holding in a std::set. I'm not sure what I'm doing wrong.
Is there anything special I have to watch out for when storing a
boost::shared_ptr in an STL container?

Regards,
Matthias
 
K

Kai-Uwe Bux

Matthias said:
Hm, yes probably you're right. It's just that I'm getting segfaults on
program exit, and it's almost certainly due to the smart pointers I'm
holding in a std::set. I'm not sure what I'm doing wrong.
Is there anything special I have to watch out for when storing a
boost::shared_ptr in an STL container?

Regards,
Matthias

Could you post a (small if possible) piece of code that demonstrates the
problem?


Best

Kai-Uwe Bux
 
M

Matthias Kaeppler

Kai-Uwe Bux said:
> Could you post a (small if possible) piece of code that demonstrates the
problem?

Sure. I have started a new thread dealing with the problem directly, I
will post the relevant parts of my code there.
 
A

Axter

Matthias said:
Hi,

I was wondering, since STL containers are based around copying, whether
it's a good idea to use reference counted smart pointers, such as
boost::shared_ptr in STL containers.
I can't store the objects directly in a container, because they must
not be duplicated, so I have to use pointers. I'm just not certain about
using raw pointers or some kind of smart pointer.

Regards,
Matthias

If you want to make sure you don't have duplicates, consider using
either a clone smart pointer or a COW smart pointer.
See following links:
http://code.axter.com/copy_ptr.h
http://code.axter.com/cow_ptr.h
http://code.axter.com/clone_ptr.h

Both the copy_ptr and clone_ptr class will make sure you only have
unique pointers, and have strict pointer ownership logic, which means
they do NOT share their pointers.

cow_ptr does share the pointer, until an attempt is made to access the
non-constant version of the pointer. At that point, it will clone
(copy) itself.
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top