3 ms·
If using something like shared_ptr in C++0x, it does come with some drawbacks (due to not having a strict ownership) as mentioned by Bjarne Stroustrup[1]: Plea
by yareally 13y ago
If using something like shared_ptr in C++0x, it does come with some drawbacks (due to not having a strict ownership) as mentioned by Bjarne Stroustrup[1]:
Please don't thoughtlessly replace pointers with shared_ptrs in an attempt to prevent memory leaks; shared_ptrs are not a panacea nor are they without costs:
-a circular linked structure of shared_ptrs will cause a memory leak (you'll need some logical complication to break the circle, e.g. using a weak_ptr)
-"shared ownership objects" tend to stay "live" for longer than scoped objects (thus causing higher average resource usage)
-shared pointers in a multi-threaded environment can be expensive (because of the need to avoid data races on the use count)
-a destructor for a shared object does not execute at a predictable time, and the algorithms/logic for the update of any shared object is easier to get wrong than for an object that's not shared
[1] http://www.stroustrup.com/C++11FAQ.html#std-shared_ptr http://www.stroustrup.com/C++11FAQ.html#std-shared_ptr
- marpalmin 13y agohttp://www.webkit.org/coding/RefPtr.html http://www.webkit.org/coding/RefPtr.html http://www.chromium.org/developers/smart-pointer-guidelines http://www.chromium.org/developers/smart-pointer-guidelines