shared-ptr

shared_ptr Assertion px != 0 failed

。_饼干妹妹 提交于 2019-12-09 14:02:18
问题 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

Make_shared - own implementation

核能气质少年 提交于 2019-12-09 12:59:42
问题 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; }

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

僤鯓⒐⒋嵵緔 提交于 2019-12-09 11:17:13
问题 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? 回答1: If we check out cppreference page for std::shared_ptr they state

how boost::~shared_ptr works?

我是研究僧i 提交于 2019-12-09 09:08:45
问题 when reading "Beyond the C++ Standard Library: An Introduction to Boost " ,I got a very interesting example: class A { public: virtual void sing()=0; protected: virtual ~A() {}; }; class B : public A { public: virtual void sing( ) { std::cout << "Do re mi fa so la"<<std::endl;; } }; and I do some testing: int main() { //1 std::auto_ptr<A> a(new B); //will not compile ,error: ‘virtual A::~A()’ is protected //2 A *pa = new B; delete pa; //will not compile ,error: ‘virtual A::~A()’ is protected

`weak_ptr::expired` behavior in the dtor of the object

爱⌒轻易说出口 提交于 2019-12-09 08:05:31
问题 Consider the following code: #include <iostream> #include <memory> using namespace std; class T; std::weak_ptr<T> wptr; class T { public: T() { } ~T() { std::cout << "in dtor" << std::endl; std::cout << (wptr.expired() ? "expired" : "not expired") << std::endl; } }; int main() { { auto ptr = std::make_shared<T>(); wptr = ptr; std::cout << (wptr.expired() ? "expired" : "not expired") << std::endl; } return 0; } In this code, I was trying to find out if weak_ptr s are expired in the objects

Why are two raw pointers to the managed object needed in std::shared_ptr implementation?

谁都会走 提交于 2019-12-09 08:01:25
问题 Here's a quote from cppreference's implementation note section of std::shared_ptr , which mentions that there are two different pointers(as shown in bold) : the one that can be returned by get() , and the one holding the actual data within the control block. In a typical implementation, std::shared_ptr holds only two pointers: the stored pointer (one returned by get() ) a pointer to control block The control block is a dynamically-allocated object that holds: either a pointer to the managed

Why would I std::move an std::shared_ptr?

孤者浪人 提交于 2019-12-09 04:00:19
问题 I have been looking through the Clang source code and I found this snippet: void CompilerInstance::setInvocation( std::shared_ptr<CompilerInvocation> Value) { Invocation = std::move(Value); } Why would I want to std::move an std::shared_ptr ? Is there any point transferring ownership on a shared resource? Why wouldn't I just do this instead? void CompilerInstance::setInvocation( std::shared_ptr<CompilerInvocation> Value) { Invocation = Value; } 回答1: I think that the one thing the other

Smart pointers for graph representation (vertex neighbors) in C++11

依然范特西╮ 提交于 2019-12-09 01:45:50
问题 I was wondering how to use C++11 smart pointers correctly for graph representations. Suppose, you have a graph structure which contains a vector of all its vertices. Furthermore, you have a structure/class of vertex. This vertex contains a vector of all its neighbors (adjacency list). My question is: which types of pointers/smart pointers should I use, to represent this graph? For binary trees, I read, for parent nodes you should use raw pointers. Because a node doesn't own its parent. The

c++ exception safety in constructor

心已入冬 提交于 2019-12-08 21:38:34
问题 What about following code MyClass a(new Foo(), new Bar()); if "new Foo()" is successful, but "new Bar()" throws, will Foo leak? Is taking std::unique_ptr<Foo> or std::shared_ptr<Foo> as parameters, enough to prevent the leak? 回答1: if "new Foo()" is successful, but "new Bar()" throws, does Foo will leak? Yes. Is taking [...] as parameters, enough to prevent the leak? Not necessarily. It depends on how you pass the parameters. For instance, even supposed your class constructor looks like this:

What is the cyclic dependency issue with shared_ptr?

十年热恋 提交于 2019-12-08 20:13:43
问题 I read about shared pointers and understood how to use. But I never understood the cyclic dependency problem with shared pointers and how weak pointers are going to fix those issues. Can any one please explain this problem clearly? 回答1: The problem isn't that complex. Let --> represent a shared pointer: The rest of the program --> object A --> object B ^ | \ | \ v object C So we've got ourselves a circular dependency with shared pointers. What's the reference count of each object? A: 2 B: 1 C