weak-ptr

Better shared_ptr by distinct types for “ownership” and “reference”?

*爱你&永不变心* 提交于 2021-02-05 10:44:05
问题 I would like to use a pointer-like object Ownership<Type> m_foo for the owning object and handle Reference<Type> m_someFoo as a classical "pointer" in another context, whereas my Reference should know when the original object does not exist anymore (e.g. by returning nullptr) and it should furthermore be possible to prevent the original object from deletion for a small period of time (locking). I know that shared_ptr (Ownership) and weak_ptr (Reference) provide similar functionality. However,

Should my API functions take shared_ptr or weak_ptr

[亡魂溺海] 提交于 2021-01-27 05:04:27
问题 I am currently designing an API and I am not sure whether my functions should take shared_ptr or weak_ptr . There are widgets that contain viewers. The viewers have a function add_painter which adds a painter to the viewer. When a viewer needs to redraw, it uses its painters to draw into a buffer and displays the result. I came to the conclusion that the viewers should hold the painters using weak_ptr : A painter may be used in multiple viewers, so the viewer cannot own the painter. Deleting

About “circular reference”, I used weak_ptr but memory leak still happened

一笑奈何 提交于 2020-06-26 07:42:32
问题 I read: How to avoid memory leak with shared_ptr? I know that I need to use weak_ptr to avoid circular reference . So I created a little program to play circular reference . Following object(spyder) will be invoke class spyder { public: spyder(std::string _name): m_name(_name), finger(nullptr) { } inline const std::string ask_name() const{ return m_name; } std::shared_ptr<spyder> finger; private: std::string m_name; }; I invoke spyder in my main code with shared_ptr and weak_ptr: int main(){

about race condition of weak_ptr

北城余情 提交于 2020-01-14 06:56:50
问题 1. i posted the question(About thread-safety of weak_ptr) several days ago,and I have the other related question now. If i do something like this,will introduce a race condition as g_w in above example ?(my platform is ms 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() //f4 run in main thread { std::shared_ptr<int> p_s = std::make_shared<int>(1); g_w = p_s; std::thread th(f3); // f3 run in the other

Boost shared_from_this and destructor

眉间皱痕 提交于 2020-01-12 10:08:46
问题 I found that it is not allowed to call shared_from_this in the destructor from a class: https://svn.boost.org/trac/boost/ticket/147 This behavior is by design. Since the destructor will destroy the object, it is not safe to create a shared_ptr to it as it will become dangling once the destructor ends. I understand the argument, but what if I need a "shared_from_this" pointer for cleaning up references ( not for sharing owner ship). Here is an example where I'm not using shared_ptr: class A{

How to make a c++11 std::unordered_set of std::weak_ptr

自作多情 提交于 2019-12-30 08:13:17
问题 I have a set like this: set<weak_ptr<Node>, owner_less<weak_ptr<Node> > > setName; It works fine. But I would like to change it to an unordered set. However, I get about six pages of errors when I do that. Any ideas how to do that? After looking through all the pages of error messages I found to lines that might help. /usr/include/c++/4.7/bits/functional_hash.h:60:7: error: static assertion failed: std::hash is not specialized for this type /usr/include/c++/4.7/bits/stl_function.h: In

std::enable_shared_from_this: is it allowed to call shared_from_this() in destructor?

可紊 提交于 2019-12-30 08:08:48
问题 #include <memory> #include <iostream> struct A : public std::enable_shared_from_this<A> { ~A() { auto this_ptr = shared_from_this(); // std::bad_weak_ptr exception here. std::cout << "this: " << this_ptr; } }; int main() { auto a = std::make_shared<A>(); a.reset(); return 0; } I'm getting std::bad_weak_ptr exception when calling shared_from_this() . Is it by design? Yes, it might be dangerous as this pointer can't be used after the destructor returns, but I don't see a reason why it would be

How does weak_ptr work?

时间秒杀一切 提交于 2019-12-28 02:26:26
问题 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. 回答1: 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

Assigning shared_ptr to weak_ptr

杀马特。学长 韩版系。学妹 提交于 2019-12-20 06:14:05
问题 I want to assign constructed shared_ptr to weak_ptr: std::weak_ptr<void> rw = std::shared_ptr<void>(operator new(60), [](void *pi) { operator delete(pi); }); But, when I do rw.expired() , it shows expired means it is empty. Any suggestions where I am going wrong? Thanks in advance. 回答1: Purpose of std::shared_ptr is to release managed object when last shared pointer which points to it is destroyed or reassigned to somewhere else. You created a temporary shared ptr, assgned it to std::weak_ptr

How to get rid of weak_ptrs in a container

*爱你&永不变心* 提交于 2019-12-19 19:44:46
问题 I have a class that stores weak_ptrs in a container and later does something if the weak_ptr is not expired: class Example { public: void fill(std::shared_ptr<int> thing) { member.push_back(thing); } void dosomething() const { for (const auto& i : member) if (!i.expired()) ;// do something. the weak_ptr will not be locked } private: std::vector<std::weak_ptr<int>> member; }; If Example is an object that lives forever and fill is used regularily, the vector allocates memory for elements