views:

78

answers:

2

I just performed Allocation Profiling about how many objects of each type are in my application. I am using boost::shared_ptr extensively.

I found a large number of sp_counted_impl_p objects allocated, each occupying 16 bytes. How many of sp_counted_impl_p objects can be expect per shared_ptr? Does someone have an idea?

+2  A: 

For what I can see in the implementation, just one per shared_ptr. However, note that there are more objects used by boost internally, that may use this counted class directly or shared_ptr itself. Also, if you use the boost.serialization framework, it is also based on this class/mechanism. Anyway, by "a large number", how many of them are there?

Diego Sevilla
hmmm ... I now think I have just one per shared_ptr, but I am collecting a large number of objects! Thanks for your answer.
Amit Kumar
+1  A: 

There should be one per object you point to with a shared_ptr. If there are more of them, you're probably not using shared_ptr properly and you're begging for troubles like double-free errors.

jpalecek
hmmm ... I now think I have just one per shared_ptr, but I am collecting a large number of objects! I ran valgrind before, which told me atleast I don't have double-free errors. Thanks for your answer, though.
Amit Kumar
You cannot have more objects than pointers.
jpalecek