smart-pointers

How does a weak_ptr know that the shared resources has expired?

痞子三分冷 提交于 2019-11-30 18:11:14
Considering the following code: #include <memory> #include <iostream> using namespace std; struct MySharedStruct { int i; }; void print_value_of_i(weak_ptr<MySharedStruct> weakPtr) { if (shared_ptr<MySharedStruct> sp = weakPtr.lock()) { cout << "Value of i = " << sp->i << endl; } else { cout << "Resource has expired"; } } int main() { shared_ptr<MySharedStruct> sharedPtr(new MySharedStruct() ); sharedPtr->i = 5; weak_ptr<MySharedStruct> weakPtr; weakPtr = sharedPtr; print_value_of_i(weakPtr); sharedPtr.reset(new MySharedStruct() ); // <<----- How does weak_ptr know it has expired after this

Getting a unique_ptr for a class that inherits enable_shared_from_this

只谈情不闲聊 提交于 2019-11-30 17:25:19
问题 Usually I prefer returning unique_ptr from Factories. Recently I came to the problem of returning a unique_ptr for a class that inherits enable_shared_from_this . Users of this class may accidentally cause a call to shared_from_this() , though it is not owned by any shared_ptr , which results with a std::bad_weak_ptr exception (or undefined behavior until C++17, which is usually implemented as an exception). A simple version of the code: class Foo: public enable_shared_from_this<Foo> { string

How can I create a smart pointer that locks and unlocks a mutex?

大憨熊 提交于 2019-11-30 14:48:55
问题 I have a threaded class from which I would like to occasionally acquire a pointer an instance variable. I would like this access to be guarded by a mutex so that the thread is blocked from accessing this resource until the client is finished with its pointer. My initial approach to this is to return a pair of objects: one a pointer to the resource and one a shared_ptr to a lock object on the mutex. This shared_ptr holds the only reference to the lock object so the mutex should be unlocked

How to approach copying objects with smart pointers as class attributes?

99封情书 提交于 2019-11-30 14:27:24
From the boost library documentation I read this: Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed. I have a very simple problem: I want to use RAII for pointer attributes of a class that is Copyable and Assignable. The copy and assignment operations should be deep: every object should have its own copy of the actual data. Also, RTTI needs to be available for the attributes (their type may also be determined at runtime). Should I be searching for an implementation of a Copyable smart pointer (the

Find memory leaks caused by smart pointers

倖福魔咒の 提交于 2019-11-30 13:43:13
问题 Does anybody know a "technique" to discover memory leaks caused by smart pointers? I am currently working on a large project written in C++ that heavily uses smart pointers with reference counting. Obviously we have some memory leaks caused by smart pointers, that are still referenced somewhere in the code, so that their memory does not get free'd. It's very hard to find the line of code with the "needless" reference, that causes the corresponding object not to be free'd (although it's not of

How to do perform a dynamic_cast with a unique_ptr?

谁都会走 提交于 2019-11-30 11:03:28
I have a class hierarchy as follows: class BaseSession : public boost::enable_shared_from_this<BaseSession> class DerivedSessionA : public BaseSession class DerivedSessionB : public BaseSession Within the derived class functions, I regularly call functions like this: Func(boost::dynamic_pointer_cast<DerivedSessionA>(shared_from_this())); Since I was working with shared_ptr to manage the sessions, this was working fine. Recently, I discovered that my use of shared_ptr is not optimal for this case. That is because these sessions are singleton objects that maintain one socket per client. If

How to enable_shared_from_this of both parent and derived

人走茶凉 提交于 2019-11-30 10:58:26
问题 I have simple base and derived class that I want both have shared_from_this() . This simple solution: class foo : public enable_shared_from_this<foo> { void foo_do_it() { cout<<"foo::do_it\n"; } public: virtual function<void()> get_callback() { return boost::bind(&foo::foo_do_it,shared_from_this()); } virtual ~foo() {}; }; class bar1 : public foo , public enable_shared_from_this<bar1> { using enable_shared_from_this<bar1>::shared_from_this; void bar1_do_it() { cout<<"foo::do_it\n"; } public:

What is the right smart pointer to have multiple strong references and allow mutability?

若如初见. 提交于 2019-11-30 09:52:10
问题 I want to have a structure on the heap with two references; one for me and another from a closure. Note that the code is for the single-threaded case: use std::rc::Rc; #[derive(Debug)] struct Foo { val: u32, } impl Foo { fn set_val(&mut self, val: u32) { self.val = val; } } impl Drop for Foo { fn drop(&mut self) { println!("we drop {:?}", self); } } fn need_callback(mut cb: Box<FnMut(u32)>) { cb(17); } fn create() -> Rc<Foo> { let rc = Rc::new(Foo { val: 5 }); let weak_rc = Rc::downgrade(&rc)

Why can operator-> be overloaded manually?

ぃ、小莉子 提交于 2019-11-30 08:29:32
Wouldn't it make sense if p->m was just syntactic sugar for (*p).m ? Essentially, every operator-> that I have ever written could have been implemented as follows: Foo::Foo* operator->() { return &**this; } Is there any case where I would want p->m to mean something else than (*p).m ? operator->() has the bizarre distinction of implicitly being invoked repeatedly while the return type allows it . The clearest way to show this is with code: struct X { int foo; }; struct Y { X x; X* operator->() { return &x; } }; struct Z { Y y; Y& operator->() { return y; } }; Z z; z->foo = 42; // Works! Calls

Find memory leaks caused by smart pointers

寵の児 提交于 2019-11-30 08:27:57
Does anybody know a "technique" to discover memory leaks caused by smart pointers? I am currently working on a large project written in C++ that heavily uses smart pointers with reference counting. Obviously we have some memory leaks caused by smart pointers, that are still referenced somewhere in the code, so that their memory does not get free'd. It's very hard to find the line of code with the "needless" reference, that causes the corresponding object not to be free'd (although it's not of use any longer). I found some advice in the web, that proposed to collect call stacks of the increment