weak-ptr

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

How to use weak_ptr in swig?

耗尽温柔 提交于 2019-11-30 17:59:23
问题 SWIG homepage says shared_ptr is specially handled, but weak_ptr not. Does it means weak_ptr supporting has some bug/issue in SWIG? If it's ok to use, how to use it? Can anybody please give a sample .i code? Thanks very much. 回答1: weak_ptr doesn't need any special support in SWIG to use. The operations that need special support in SWIG for shared_ptr are dereferencing and passing into functions. This is because you never directly dereference or create a weak_ptr . Instead when you're using it

boost, shared ptr Vs weak ptr? Which to use when?

白昼怎懂夜的黑 提交于 2019-11-29 19:36:37
In my current project I am using boost::shared_ptr quite extensively. Recently my fellow team mates have also started using weak_ptr . I don't know which one to use and when. Apart from this, what should I do if I want to convert weak_ptr to shared_ptr . Does putting a lock on weak_ptr to create a shared_ptr affect my code in other thread? Narfanator In general and summary, Strong pointers guarantee their own validity. Use them, for example, when: You own the object being pointed at; you create it and destroy it You do not have defined behavior if the object doesn't exist You need to enforce

boost::weak_ptr<T>.lock() Crashes with a SIGSEGV Segmentation Fault

 ̄綄美尐妖づ 提交于 2019-11-29 12:48:43
(EDIT) Environment: plee@sos-build:/usr/local/include/boost$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 11.10 Release: 11.10 Codename: oneiric plee@sos-build:/usr/local/include/boost$ uname -a Linux sos-build 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux plee@sos-build:/usr/local/include/boost$ plee@sos-build:/usr/local/include/boost$ cat version.hpp // BOOST_LIB_VERSION must be defined to be the same as BOOST_VERSION #define BOOST_LIB_VERSION "1_47" I have been working on a server-side project. I use

Binding to a weak_ptr

谁说我不能喝 提交于 2019-11-29 01:24:04
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() { std::weak_ptr<MyClass> thisWeakPtr(shared_from_this()); return std::function<void()>(std::bind(

shared_ptr and cyclic references

谁说我不能喝 提交于 2019-11-28 23:46:32
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. cout<<p1.use_count()<<'\n'; // putchar('\n'); cout<<endl; } which outputs 4 5 4 ~A : 4 Is it that I've

boost, shared ptr Vs weak ptr? Which to use when?

你离开我真会死。 提交于 2019-11-28 15:17:59
问题 In my current project I am using boost::shared_ptr quite extensively. Recently my fellow team mates have also started using weak_ptr . I don't know which one to use and when. Apart from this, what should I do if I want to convert weak_ptr to shared_ptr . Does putting a lock on weak_ptr to create a shared_ptr affect my code in other thread? 回答1: In general and summary, Strong pointers guarantee their own validity. Use them, for example, when: You own the object being pointed at; you create it

boost::weak_ptr<T>.lock() Crashes with a SIGSEGV Segmentation Fault

霸气de小男生 提交于 2019-11-28 06:17:30
问题 (EDIT) Environment: plee@sos-build:/usr/local/include/boost$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 11.10 Release: 11.10 Codename: oneiric plee@sos-build:/usr/local/include/boost$ uname -a Linux sos-build 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux plee@sos-build:/usr/local/include/boost$ plee@sos-build:/usr/local/include/boost$ cat version.hpp // BOOST_LIB_VERSION must be defined to be the same as

Equality-compare std::weak_ptr

回眸只為那壹抹淺笑 提交于 2019-11-28 04:32:16
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_shared<int>(43); std::weak_ptr<int> w1(s1); s1.reset(); auto s2 = std::make_shared<int>(41); std::weak

How can I use a std::map with std::weak_ptr as key?

▼魔方 西西 提交于 2019-11-28 03:01:18
问题 How can I use std::weak_ptr as key for a std::map as shown in the following code? #include <map> #include <memory> int main() { std::map< std::weak_ptr<int>, bool > myMap; std::shared_ptr<int> sharedptr(new int(5)); std::weak_ptr<int> weakptr = sharedptr; myMap[weakptr] = true; return 0; } The above program doesn't build and trying to compile it gives many error messages such as: 1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xfunctional(125): error C2784: 'bool std: