shared-ptr

how to return shared_ptr to current object from inside the “this” object itself

流过昼夜 提交于 2019-12-10 11:05:29
问题 I have an instance of View class (instantiated somewhere in the Controller owning object using shared_ptr) class ViewController { protected: std::shared_ptr<View> view_; }; This view has also method "hitTest()" that should return this view's shared_ptr to the outside world class View { ... public: std::shared_ptr<UIView> hitTest(cocos2d::Vec2 point, cocos2d::Event * event) { ... }; How can I implement this method from inside the View? It should return this VC's shared_ptr to outside?

Registering weak_ptr observer in constructor

别来无恙 提交于 2019-12-10 10:56:19
问题 I'm trying to rewrite our Observer / Observable implementation to use std::shared_ptr/std::weak_ptr to get rid of some nasty race conditions currently present in the code. Typically, the observers registers themselves when some condition is met or when they are constructing child objects like so: // Used to be raw 'this' now child instead derives a weak_ptr and stores it child->addObserver(shared_from_this()) And unregisters themselves in destructor like so: child->removeObserver(this); //

shared_ptr and use_count

纵然是瞬间 提交于 2019-12-10 09:38:51
问题 In the following code snippet: shared_ptr<int> p; { p = shared_ptr<int>(new int); cout<<p.use_count()<<endl; } cout<<p.use_count()<<endl; The output comes out to be 1 1 I don't understand why the 1st output is 1 -- shouldn't it be 2 ? 回答1: The temporary object's lifetime does not last long enough for the first p.use_count() to return 2. The temporary object is destroyed first, relinquishing its ownership on anything it owned. Furthermore, since the temporary is an rvalue, the assignment to p

Making shared_ptr lose ownership of memory

狂风中的少年 提交于 2019-12-10 09:23:27
问题 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

intrusive_ptr: Why isn't a common base class provided?

廉价感情. 提交于 2019-12-10 03:15:07
问题 boost::intrusive_ptr requires intrusive_ptr_add_ref and intrusive_ptr_release to be defined. Why isn't a base class provided which will do this? There is an example here: http://lists.boost.org/Archives/boost/2004/06/66957.php, but the poster says "I don't necessarily think this is a good idea". Why not? Update: I don't think the fact that this class could be misused with Multiple Inheritance is reason enough. Any class which derives from multiple base classes with their own reference count

Does make_shared do a default initialization (zero-init) for each member variable

独自空忆成欢 提交于 2019-12-10 01:36:37
问题 Take an ordinary struct (or class) with Plain Old Data types and objects as members. Note that there is no default constructor defined. struct Foo { int x; int y; double z; string str; }; Now if I declare an instance f on the stack and attempt to print its contents: { Foo f; std::cout << f.x << " " << f.y << " " << f.z << f.str << std::endl; } The result is garbage data printed for x, y, and z. And the string is default initialized to be empty. As expected. If I create an instance of a shared

new and make_shared for shared pointers

别等时光非礼了梦想. 提交于 2019-12-09 18:14:04
问题 I came across this post and one of the answers by @kerek SB states std::shared_ptr<Object> p1 = std::make_shared<Object>("foo"); std::shared_ptr<Object> p2(new Object("foo")); In your code, the second variable is just a naked pointer, not a shared pointer at all. Now on the meat. make_shared is (in practice) more efficient, because it allocates the reference control block together with the actual object in one single dynamic allocation. By contrast, the constructor for shared_ptr that takes a

How to access target of std::tr1::shared_ptr in GDB

情到浓时终转凉″ 提交于 2019-12-09 16:55:19
问题 How can I access target of a std::tr1::shared_ptr in GDB. This doesn't work: (gdb) p sharedPtr->variableOfTarget If I try with the pointer object itself ( p sharedPtr ) I get something like this: $1 = std::tr1::shared_ptr (count 2) 0x13c2060 With a normal pointer I can do p *ptr and get all the data or p ptr->variable for just one variable. I'm on Centos 6.5, GCC 4.4.7-4.el6 and GDB 7.2-64.el6_5.2. 回答1: Try with (gdb) p (*sharedPtr.get()) that function returns the a pointer to the object

Weak pointer to this in constructor

可紊 提交于 2019-12-09 16:40:57
问题 I understand it is not possible to obtain a shared_ptr by calling shared_from_this() from the constructor of a class, as the object is not yet constructed. Is it possible however to obtain a weak_ptr to the object from the constructor? Some boost forum posts discussing a "weak_from_raw()" method suggests that it may be possible. Edit: Boost form discussing weak_from_raw http://lists.boost.org/boost-users/2010/08/61541.php 回答1: I think what you're referencing is this. Which seems not to have

c++0x std::shared_ptr vs. boost::shared_ptr

扶醉桌前 提交于 2019-12-09 16:14:09
问题 I have a c++ code which heavily uses shared_ptr and STL. A common header says #include<boost/shared_ptr.hpp> using boost::shared_ptr; // for shared_ptr using namespace std; // for STL I wanted to switch to c++0x now to make use of the language features, using gcc 4.6 with -std=c++0x . There is however std::shared_ptr now as well, leading to ambiguity for unspecified shared_ptr ( boost::shared_ptr vs std::shared_ptr ). When switching to std::shared_ptr instead, like this: #include<memory>