shared-ptr

Why allow shared_ptr<T[N]>?

家住魔仙堡 提交于 2019-12-03 23:55:46
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, shall be well-formed, shall have well defined behavior, and shall not throw exceptions. When T is U[N] , Y

shared_ptr Assertion px != 0 failed

こ雲淡風輕ζ 提交于 2019-12-03 22:07:21
I have a fairly complex multi threaded application (server) that from time to time will crash due to an assert: /usr/include/boost/smart_ptr/shared_ptr.hpp:418: T* boost::shared_ptr< <template-parameter-1-1> >::operator->() const [with T = msg::Player]: Assertion `px != 0' failed. I have been unable to pinpoint the cause and was wondering if this is a problem with boost::shared_ptr or it is me? I tried g++ 4.4.3-4ubuntu5 and llvm-g++ (GCC) 4.2.1 with optimization and without optimization and libboost1.40-dev (= 1.40.0-4ubuntu4). There should be no problem with using boost::shared_ptr as long

Wrapping std::vector of boost::shared_ptr in SWIG for Python

浪子不回头ぞ 提交于 2019-12-03 20:13:23
EDIT: Solved, my mistake; explained in my answer. I have this: std::vector < boost::shared_ptr < Entity > > entities; and I try to expose it through SWIG like this: %include "boost_shared_ptr.i" %include "std_vector.i" %shared_ptr(Entity) %include <Entity.h> namespace std { %template(EntityVector) vector<boost::shared_ptr<Entity> >; }; %include <TheFileWithEntities.h> However, in Python entities ends up being a tuple: import MyModule print type(MyModule.cvar.entities) # Output: (type 'tuple') I've Googled for this, but could not find any concrete examples on how to wrap this. One page gave a

bad weak pointer when base and derived class both inherit from boost::enable_shared_from_this

↘锁芯ラ 提交于 2019-12-03 19:40:35
问题 I have a base class which derives from boost::enable_shared_from_this, and then another class which derives from both the base class and boost::enable_shared_from_this: #include <boost/enable_shared_from_this.hpp> #include <boost/shared_ptr.hpp> using namespace boost; class A : public enable_shared_from_this<A> { }; class B : public A , public enable_shared_from_this<B> { public: using enable_shared_from_this<B>::shared_from_this; }; int main() { shared_ptr<B> b = shared_ptr<B>(new B());

Smart pointers + cycles + “->”

南楼画角 提交于 2019-12-03 17:26:05
问题 Sometimes I'm really sure that I want to have circular dependence of pointers, and every object on cycle should be able to use his pointer (so it can't be weak_ptr). My question is: Does this mean that I have bad design? What if I want to implement graph? Can I use smart pointers? In graphs there are cycles, but with weak_ptr I can't use "->". What can I do? I read some articles, reference and topics on StackOverflow, but it looks like I still don't get smart pointers. Really, why doesn't

Make_shared - own implementation

做~自己de王妃 提交于 2019-12-03 16:12:10
I am trying to make my own implementation of shared_ptr . I have problems with make_shared . The main feature of std::make_shared that it allocates counter block and object in continuous block of memory. How I can do the same? I tried do something like that: template<class T> class shared_ptr { private: class _ref_cntr { private: long counter; public: _ref_cntr() : counter(1) { } void inc() { ++counter; } void dec() { if (counter == 0) { throw std::logic_error("already zero"); } --counter; } long use_count() const { return counter; } }; template<class _T> struct _object_and_block { _T object;

What is the need for enable_shared_from_this? [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-03 15:35:32
This question already has an answer here: What is the usefulness of `enable_shared_from_this`? 6 answers I am new to C++11 and I came across enable_shared_from_this. I do not understand what it is trying to achieve? So I have a program that uses enable_shared_from_this. struct TestCase: enable_shared_from_this<TestCase> { std::shared_ptr<testcase> getptr() { return shared_from_this(); } ~TestCase() { std::cout << "TestCase::~TestCase()"; } }; int main() { std::shared_ptr<testcase> obj1(new TestCase); std::shared_ptr<testcase> obj2 = obj1->getptr(); // The above can be re written as below //

std::shared_ptr Exception Safety

孤人 提交于 2019-12-03 14:33:45
问题 I just realised reading this page that the constructor of std::shared_ptr with a single pointer argument is not noexcept. Hence the following code contains a possible memory leak: std::shared_ptr<int> p3 (new int); The reasonning is that two allocations could occure: The first one before the call to the constructor The second one in the constructor of shared_ptr (This is what happens in VS 2012 for example) Two questions here: Is it true that if the second allocation throws an exception, the

What's the overhead from shared_ptr being thread-safe?

我的梦境 提交于 2019-12-03 14:01:25
std::shared_ptr is guaranteed to be thread-safe. I don't know what mechanism the typical implementations use to ensure this, but surely it must have some overhead. And that overhead would be present even in the case that your application is single-threaded. Is the above the case? And if so, does that means it violates the principle of "you don't pay for what you don't use", if you aren't using the thread-safety guarantees? If we check out cppreference page for std::shared_ptr they state the following in the Implementation notes section: To satisfy thread safety requirements, the reference

How do we return a unique_pointer member from a member function?

余生颓废 提交于 2019-12-03 13:33:06
I have a base class with a pointer member. I would have to make an educated guess to determine whether it should be an unique_ptr or a shared_ptr . None of them seems to solve my particular use case. class Base { public: Base(): pInt(std::unique_ptr<int>(new int(10))) {}; virtual std::unique_ptr<int> get() = 0; //Base(): pInt(std::shared_ptr<int>(new int(10))) {}; // Alternate implementation //virtual std::shared_ptr<int> get() = 0; // Alternate implementation private: std::unique_ptr<int> pInt; //std::shared_ptr<int> pInt; // Alternate implementation }; The base class has been derived onto