shared-ptr

dynamic cast of a shared_ptr

冷暖自知 提交于 2019-12-05 19:01:20
I have a few classes of which I've made std::shared_ptr versions, as follows: typedef std::shared_ptr<MediaItem> MediaItemPtr; typedef std::shared_ptr<ImageMediaItem> ImageMediaItemPtr; class MediaItem { //stuff here } class ImageMediaItem : public MediaItem { //more stuff here } Internally, I pass everything around as a MediaItemPtr object, but when I try to cast to a ImageMediaItemPtr, nothing I try seems to work. For example: ImageMediaItemPtr item = std::dynamic_pointer_cast<ImageMediaItemPtr>(theItem); //theItem is MediaItemPtr Fails with error C2440: 'initializing' : cannot convert from

Making shared_ptr lose ownership of memory

放肆的年华 提交于 2019-12-05 15:33:49
I have a shared_ptr<MyProto> which I pass around. Eventually, in certain situations, I want pass the raw pointer to a function which then becomes the memory owner. In those cases the shared_ptr isn't responsible anymore for freeing the memory because the function I call took ownership. How do I get the shared_ptr to lose ownership? The reason I want to have the shared_ptr lose ownership is that I want to use protocol buffer's AddAllocated functionality which takes an already allocated pointer and assumes ownership of it. Example: shared_ptr<MyProto> myProtoSharedPtr = // by this point this is

Why must shared_ptr<> allocate for the control block and managed object separately?

空扰寡人 提交于 2019-12-05 13:00:44
This linked question asked if the make_shared<> function and the shared_ptr<> constructor differ. What happens when using make_shared Part of the answer was that make_shared<> will usually allocate memory for both the pointed to object and smart pointer control block in a single allocation. The shared_ptr<> constructors use two allocations. cppreference states that the constructors "must" do so but no reason is given. Why is this? Is it for some reason impossible? Or is it forbidden by the standard for some other reason? Think about how the std::shared_ptr constructor works: std::shared_ptr

bad_weak_ptr when calling shared_from_this() in base class

可紊 提交于 2019-12-05 10:47:04
I have a SuperParent class, a Parent class (derived from SuperParent ) and both contain a shared_ptr to a Child class (which contains a weak_ptr to a SuperParent ). Unfortunately, I'm getting a bad_weak_ptr exception when trying to set the Child 's pointer. The code is the following: #include <boost/enable_shared_from_this.hpp> #include <boost/make_shared.hpp> #include <boost/shared_ptr.hpp> #include <boost/weak_ptr.hpp> using namespace boost; class SuperParent; class Child { public: void SetParent(shared_ptr<SuperParent> parent) { parent_ = parent; } private: weak_ptr<SuperParent> parent_; };

Why is raw pointer to shared_ptr construction allowed in all cases?

岁酱吖の 提交于 2019-12-05 10:25:13
I was reading Top 10 dumb mistakes to avoid with C++11 smart pointer . Number #5 reads: Mistake # 5 : Not assigning an object(raw pointer) to a shared_ptr as soon as it is created ! int main() { Aircraft* myAircraft = new Aircraft("F-16"); shared_ptr<aircraft> pAircraft(myAircraft); ... shared_ptr<aircraft> p2(myAircraft); // will do a double delete and possibly crash } and the recommendation is something like: Use make_shared or new and immediately construct the pointer with it. Ok, no doubt about it the problem and the recommendation. However I have a question about the design of shared_ptr

What happens if I reset a std::shared_ptr to itself

微笑、不失礼 提交于 2019-12-05 10:09:17
The following program crashes with a bad glibc double free error: #include <iostream> #include <memory> class foo { public: foo() { std::cout << "foo constructed" << std::endl; } ~foo() { std::cout << "foo destructed" << std::endl; } }; int main() { auto f = std::make_shared< foo >(); std::cout << "Before reset" << std::endl; f.reset( f.get() ); std::cout << "After reset" << std::endl; return 0; } From this I get the following output (followed by the glibc error): foo constructed Before reset foo destructed After reset foo destructed So obviously in this case the object is destroyed twice.

Why allow shared_ptr<T[N]>?

喜你入骨 提交于 2019-12-05 09:46:43
问题 This answer cites N4082, which shows that the upcoming changes to std::shared_ptr will allow both T[] and T[N] variants: Unlike the unique_ptr partial specialization for arrays, both shared_ptr<T[]> and shared_ptr<T[N]> will be valid and both will result in delete[] being called on the managed array of objects. template<class Y> explicit shared_ptr(Y* p); Requires : Y shall be a complete type. The expression delete[] p , when T is an array type, or delete p , when T is not an array type,

Soft (not: weak) references in C++ - Is it possible? Is there an implementation?

喜欢而已 提交于 2019-12-05 08:08:07
In C++ I'm using boost::shared_ptr and boost::weak_ptr to automatically delete objects that are no longer needed. I know these work with reference counting. In Java, memory is managed by a garbage collector, which consideres the built-in object references as strong , WeakReference as weak and SoftReference as something in between (may be collected by the GC, but may as well survive the GC), which is really handy for caching objects for some time, but throwing them away as soon as free memory is getting low. So now I'm back in C++ and I miss the comfort of having soft references. I wonder if

Same address, multiple shared_ptr counters, is it forbidden by C++ standard?

陌路散爱 提交于 2019-12-05 08:07:05
Suppose I have the need to do the following (This is just some imaginative code for discussion of the C++ standard, thus I won't discuss why I design it this way, so don't bother me with something like: your design is wrong.) T* ptr = new T; shared_ptr<T> p(ptr); shared_ptr<T> q(ptr, SomeDeleterThatDoesnotDeleteButDoSomeOtherStuff()); Suppose the logic guarantees that p or some of its copies lives longer than all copies of q , so practically there won't be any problem. My question is, is it forbidden by C++ standard, e.g. explicitly stated as UB by C++ standard, for different shared_ptr

boost::python and set::erase -> weird behaviour

╄→尐↘猪︶ㄣ 提交于 2019-12-05 07:47:39
I'm trying to store objects in a std::set. Those objects are boost::shared_ptr<>, coming from the python environment. adding values to the set won't cause any troubles. But when I try to erase a value, even though I'm passing the very same reference, it won't work. Here is an example : #include <set> #include <iostream> #include <boost/shared_ptr.hpp> #include <boost/python.hpp> using namespace std; using namespace boost; using namespace boost::python; struct Bar { Bar() {} }; struct Foo { set< shared_ptr<Bar> > v_set; shared_ptr<Bar> v_ptr; Foo() {} void add( shared_ptr<Bar> v_param ) { cout