shared-ptr

shared_from_this causing bad_weak_ptr

半腔热情 提交于 2019-11-27 13:26:23
I am trying to keep a list of connected clients in asio. I have adapted the chat server example from the docs ( http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cpp03/chat/chat_server.cpp ) and here's the important part of what I ended up with: #include <iostream> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <boost/enable_shared_from_this.hpp> #include <boost/asio.hpp> #include <set> using boost::asio::ip::tcp; class tcp_connection; std::set<boost::shared_ptr<tcp_connection>> clients; void add_client(boost::shared_ptr<tcp_connection> client) { clients

Using shared_ptr in dll-interfaces

狂风中的少年 提交于 2019-11-27 12:07:45
I have an abstract class in my dll. class IBase { protected: virtual ~IBase() = 0; public: virtual void f() = 0; }; I want to get IBase in my exe-file which loads dll. First way is to create following function IBase * CreateInterface(); and to add the virtual function Release() in IBase . Second way is to create another function boost::shared_ptr<IBase> CreateInterface(); and no Release() function is needed. Questions. 1) Is it true that the destructor and memory deallocation is called in the dll (not in exe-file) in the second case ? 2) Does the second case work well if exe-file and dll was

How to initialize a shared_ptr that is a member of a class?

自古美人都是妖i 提交于 2019-11-27 11:31:02
问题 I am not sure about a good way to initialize a shared_ptr that is a member of a class. Can you tell me, whether the way that I choose in C::foo() is fine, or is there a better solution? class A { public: A(); }; class B { public: B(A* pa); }; class C { boost::shared_ptr<A> mA; boost::shared_ptr<B> mB; void foo(); }; void C::foo() { A* pa = new A; mA = boost::shared_ptr<A>(pa); B* pB = new B(pa); mB = boost::shared_ptr<B>(pb); } 回答1: Your code is quite correct (it works), but you can use the

Deletion of pointer to incomplete type and smart pointers

微笑、不失礼 提交于 2019-11-27 11:15:48
问题 When trying to use an auto_ptr with a type that was declared with forward-declaration, like this: class A; ... std::auto_ptr<A> a; the destructor of A is not called (apparently, because auto_ptr internally delete s the underlying pointer and the destructor for an incomplete type cannot be called). However, the same code works fine and the destructor is called when using std::shared_ptr instead of std::auto_ptr . How can that be explained? 回答1: A shared_ptr can be declared with an incomplete

How is the std::tr1::shared_ptr implemented?

前提是你 提交于 2019-11-27 10:48:18
I've been thinking about using shared pointers, and I know how to implement one myself--Don't want to do it, so I'm trying std::tr1::shared_ptr ,and I have couple of questions... How is the reference counting implemented? Does it use a doubly linked list? (Btw, I've already googled, but I can't find anything reliable.) Are there any pitfalls for using the std::tr1::shared_ptr ? Emilio Garavaglia shared_ptr must manage a reference counter and the carrying of a deleter functor that is deduced by the type of the object given at initialization. The shared_ptr class typically hosts two members: a T

What are potential dangers when using boost::shared_ptr?

爷,独闯天下 提交于 2019-11-27 10:44:16
What are some ways you can shoot yourself in the foot when using boost::shared_ptr ? In other words, what pitfalls do I have to avoid when I use boost::shared_ptr ? Cyclic references: a shared_ptr<> to something that has a shared_ptr<> to the original object. You can use weak_ptr<> to break this cycle, of course. I add the following as an example of what I am talking about in the comments. class node : public enable_shared_from_this<node> { public : void set_parent(shared_ptr<node> parent) { parent_ = parent; } void add_child(shared_ptr<node> child) { children_.push_back(child); child->set

What is the difference between an empty and a null std::shared_ptr in C++?

随声附和 提交于 2019-11-27 10:43:12
The cplusplus.com shared_ptr page calls out a distinction between an empty std::shared_ptr and a null shared_ptr . The cppreference.com page doesn't explicitly call out the distinction, but uses both "empty" and comparison to nullptr in its description of std::shared_ptr behavior. Is there a difference between an empty and a null shared_ptr ? Is there any use case for such mixed-behavior pointers? Does a non-empty null shared_ptr even make sense? Would there ever be a case in normal usage (i.e. if you didn't explicitly construct one) where you could end up with an empty-but-non-null shared_ptr

shared_ptr: horrible speed

早过忘川 提交于 2019-11-27 10:33:52
When comparing two variants of pointers—classic vs. shared_ptr—I was surprised by a significant increase of the running speed of the program. For testing 2D Delaunay incremental Insertion algorithm has been used. Compiler settings: VS 2010 (release) /O2 /MD /GL, W7 Prof, CPU 3.GHZ DualCore Results: shared_ptr (C++ 0x00): N[points] t[sec] 100 000 6 200 000 11 300 000 16 900 000 36 Pointers: N[points] t[sec] 100 000 0,5 200 000 1 300 000 2 900 000 4 Running time of the shared_ptr versions is approximately 10 times longer. Is this caused by the compiler settings or C++ 0x00 shared_ptr

Example to use shared_ptr?

我的未来我决定 提交于 2019-11-27 10:13:17
Hi I asked a question today about How to insert different types of objects in the same vector array and my code in that question was gate* G[1000]; G[0] = new ANDgate() ; G[1] = new ORgate; //gate is a class inherited by ANDgate and ORgate classes class gate { ..... ...... virtual void Run() { //A virtual function } }; class ANDgate :public gate {..... ....... void Run() { //AND version of Run } }; class ORgate :public gate {..... ....... void Run() { //OR version of Run } }; //Running the simulator using overloading concept for(...;...;..) { G[i]->Run() ; //will run perfectly the right Run

Why is shared_ptr<void> legal, while unique_ptr<void> is ill-formed?

邮差的信 提交于 2019-11-27 10:12:37
问题 The question really fits in the title: I am curious to know what is the technical reason for this difference, but also the rationale ? std::shared_ptr<void> sharedToVoid; // legal; std::unique_ptr<void> uniqueToVoid; // ill-formed; 回答1: It is because std::shared_ptr implements type-erasure, while std::unique_ptr does not. Since std::shared_ptr implements type-erasure, it also supports another interesting property, viz. it does not need the type of the deleter as template type argument to the