I’ve asked a couple questions (here and here) about memory management, and invariably someone suggests that I use boost::shared_ptrs.
Given how useful they seem to be, I’m seriously considering switching over my entire application to use boost::shared_ptrs.
However, before I jump in with both feet and do this, I wanted to ask — Has anyone had any bad experiences with boost::shared_ptrs? Is there some pitfall to using them that I need to watch out for?
Right now, they seem almost too good to be true – taking care of most of my garbage collection concerns automatically. What’s the downside?
The downside is they’re not free. You especially shouldn’t use
shared_ptr/shared_arraywhenscoped_ptr/scoped_array(or plain old stack allocation) will do. You’ll need to manually break cycles withweak_ptrif you have any. The vector question you link to is one case where I might reach for ashared_ptr, the second question I would not. Not copying is a premature optimization, especially if the string class does it for you already. If the string class is reference counted, it will also be able to implement COW properly, which can’t really be done with theshared_ptr<string>approach. Usingshared_ptrwilly-nilly will also introduce ‘interface friction’ with external libraries/apis.