Boost logo

Boost Users :

Subject: Re: [Boost-users] [PtrContainer] Inserting "this" Pointers
From: Hossein Haeri (powerprogman_at_[hidden])
Date: 2010-09-22 05:27:31


Hi Binglong,

> Since you get a hand on aa already, why don't you just call
> instead:
>    v.push_back(aa);

Because my insert_to() isn't just a simple push_back(); It requires non-trivial computations beforehand.

> If you insist using insert_me() with smart pointers, as as
> Benjamin pointed out, you may need to use
> boost::enable_shared_from_this. Otherwise a shared_ptr
> instantiated with a this pointer in insert_me() is very
> dangerous, because it will eventually try to delete aa even
> if aa was created on the stack.

Yes, I can well remember this nice discussion on our other thread. My motivation for targeting pointer containers instead of STL ones in combination with smart pointers is to get rid of the messy and error-prone pointer arithmetic. (I'm still reading the documentation.) I don't still know how pointer containers internally manage the dynamically allocated pieces of memory. Thus, I'm exploring the matter and am specifically interested in knowing whether I would need to use enable_shared_from_this and make_shared for container pointers too? And, if yes, how would I need to do that if I'm about to store "this" pointers inside them?

TIA,
--Hossein


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net