weak-ptr

weak_ptr, make_shared and memory deallocation

巧了我就是萌 提交于 2019-11-27 15:48:59
问题 A control block of a shared_ptr is kept alive while there is at least one weak_ptr present. If the shared pointer was created with make_shared that implies that the whole memory of the object is kept allocated. (The object itself is properly destructed, but since the control block and the memory for the object were allocated in one chunk, as make_shared does, they can only be deallocated together.) Is my understanding correct? It seems that this behaviour represents a problem, for example in

shared_ptr and cyclic references

匆匆过客 提交于 2019-11-27 15:03:24
问题 I was trying with the cyclic references for boost::shared_ptr , and devised following sample: class A{ // Trivial class public: i32 i; A(){} A(i32 a):i(a){} ~A(){ cout<<"~A : "<<i<<endl; } }; shared_ptr<A> changeI(shared_ptr<A> s){ s->i++; cout<<s.use_count()<<'\n'; return s; } int main() { shared_ptr<A> p1 = make_shared<A>(3); shared_ptr<A> p2 = p1; shared_ptr<A> p3 = p2; shared_ptr<A> p4 = p3; p1 = p4; // 1) 1st cyclic ref. cout<<p1.use_count()<<'\n'; p1 = changeI(p4); // 2) 2nd cyclic ref.

About thread-safety of weak_ptr

对着背影说爱祢 提交于 2019-11-27 14:23:07
std::shared_ptr<int> g_s = std::make_shared<int>(1); void f1() { std::shared_ptr<int>l_s1 = g_s; // read g_s } void f2() { std::shared_ptr<int> l_s2 = std::make_shared<int>(3); std::thread th(f1); th.detach(); g_s = l_s2; // write g_s } Regarding the code above, I know different threads reading and writing the same shared_ptr leads to race conditions. But how about weak_ptr ? Is there any race condition in the code below? (My platform is Microsoft VS2013.) std::weak_ptr<int> g_w; void f3() { std::shared_ptr<int>l_s3 = g_w.lock(); //2. here will read g_w if (l_s3) { ;/..... } } void f4() { std:

How does weak_ptr work?

纵然是瞬间 提交于 2019-11-27 06:27:02
I understand how to use weak_ptr and shared_ptr . I understand how shared_ptr works, by counting the number of references in its object. How does weak_ptr work? I tried reading through the boost source code, and I'm not familiar enough with boost to understand all the things it uses. Thanks. Paul Groke shared_ptr uses an extra "counter" object (aka. "shared count" or "control block") to store the reference count. (BTW: that "counter" object also stores the deleter.) Every shared_ptr and weak_ptr contains a pointer to the actual pointee, and a second pointer to the "counter" object. To

shared_ptr and weak_ptr differences

▼魔方 西西 提交于 2019-11-27 05:56:21
I am reading Scott Meyers "Effective C++" book. It was mentioned that there are tr1::shared_ptr and tr1::weak_ptr act like built-in pointers, but they keep track of how many tr1::shared_ptrs point to an object. This is known as reference counting. This works well in preventing resource leaks in acyclic data structures, but if two or more objects contain tr1::shared_ptrs such that a cycle is formed, the cycle may keep each other's reference count above zero, even when all external pointers to the cycle have been destroyed. That's where tr1::weak_ptrs come in. My question is how cyclic data

Equality-compare std::weak_ptr

假装没事ソ 提交于 2019-11-27 05:21:52
问题 I want to compare two std::weak_ptr's or one std::weak_ptr and one std::shared_ptr for equality. What I want to know is whether the object each of the weak_ptr's/shared_ptr's point to is the same. The comparison should yield negative results not just if the addresses don't match, but also if the underlying object was deleted and then reconstructed with the same address by chance. So basically, I want this assertion to hold even if the allocator reserves the same address: auto s1 = std::make

Binding to a weak_ptr

旧时模样 提交于 2019-11-27 02:14:51
问题 Is there a way to std::bind to a std::weak_ptr ? I'd like to store a "weak function" callback that automatically "disconnects" when the callee is destroyed. I know how to create a std::function using a shared_ptr: std::function<void()> MyClass::GetCallback() { return std::function<void()>(std::bind(&MyClass::CallbackFunc, shared_from_this())); } However the returned std::function keeps my object alive forever. So I'd like to bind it to a weak_ptr : std::function<void()> MyClass::GetCallback()

About thread-safety of weak_ptr

被刻印的时光 ゝ 提交于 2019-11-26 16:43:43
问题 std::shared_ptr<int> g_s = std::make_shared<int>(1); void f1() { std::shared_ptr<int>l_s1 = g_s; // read g_s } void f2() { std::shared_ptr<int> l_s2 = std::make_shared<int>(3); std::thread th(f1); th.detach(); g_s = l_s2; // write g_s } Regarding the code above, I know different threads reading and writing the same shared_ptr leads to race conditions. But how about weak_ptr ? Is there any race condition in the code below? (My platform is Microsoft VS2013.) std::weak_ptr<int> g_w; void f3() {

shared_ptr and weak_ptr differences

风格不统一 提交于 2019-11-26 15:05:46
问题 I am reading Scott Meyers "Effective C++" book. It was mentioned that there are tr1::shared_ptr and tr1::weak_ptr act like built-in pointers, but they keep track of how many tr1::shared_ptrs point to an object. This is known as reference counting. This works well in preventing resource leaks in acyclic data structures, but if two or more objects contain tr1::shared_ptrs such that a cycle is formed, the cycle may keep each other's reference count above zero, even when all external pointers to

When is std::weak_ptr useful?

天涯浪子 提交于 2019-11-26 04:29:02
问题 I started studying smart pointers of C++11 and I don\'t see any useful use of std::weak_ptr . Can someone tell me when std::weak_ptr is useful/necessary? 回答1: A good example would be a cache. For recently accessed objects, you want to keep them in memory, so you hold a strong pointer to them. Periodically, you scan the cache and decide which objects have not been accessed recently. You don't need to keep those in memory, so you get rid of the strong pointer. But what if that object is in use