Questions tagged [enable-shared-from-this]

36 questions
3
votes
2 answers

How does shared_ptr detect that T derives from enable_shared_from_this?

I am trying to learn how shared_ptr works by implementing it from scratch, and I can't figure out how to detect T's base class. I've tried using is_base_of(), but that gives a const value, which I can't use with an if statement to set the object's…
Alex
  • 846
  • 6
  • 16
2
votes
1 answer

Question about the usage of shared_from_this() in practice

The below code snippet is seen at cppreference. I am curious about what the intention of Best::getptr() is? When should I use this method in practice? Maybe a simple demo code helps a lot. struct Best : std::enable_shared_from_this // note:…
John
  • 2,963
  • 11
  • 33
2
votes
1 answer

std::enable_shared_from_this, non-virtual destructor and public inheritance

The std::enable_shared_from_this class is a (template) mixin, recommended for use to enable creating shared pointers from a given object (or its address), which all have common ownership of the object. The thing is, that if you have an class T…
2
votes
1 answer

Bad_weak_ptr caused by call to shared_from_this with multiple inheritence

I am trying to understand why there is a bad_weak_ptr exception when calling shared_from_this. #include #include class parent : public std::enable_shared_from_this { public: void compare(std::shared_ptr
2
votes
4 answers

Why does std::enable_shared_from_this allow multiple std::shared_ptr instances?

There are several questions that cover the behavior of std::enable_shared_from_this, but I don't think that this is a duplicate. Classes that inherit from std::enable_shared_from_this carry a std::weak_ptr member. When the application creates a…
Willis Blackburn
  • 8,068
  • 19
  • 36
1
vote
2 answers

Initializing a member with type std::shared_ptr of a class A with a pointer to class B in a member function of B

I have the following code: class Cohomology; struct EMField { std::shared_ptr coh; std::array data; // other methods } class Cohomology { private: // private members public: …
1
vote
1 answer

when to use shared_from_this()

If I want to return a shared_ptr for class A, I should use shared_from_this() feature. But I think this will work after I defined a shared_ptr for A already. So if another place wants a shared_ptr I just use the one I made. I wonder when to use…
1
vote
1 answer

pybind11, compile fails on binding Trampolines and multiple inheritance with std::enable_shared_from_this

I have a compile problem with multiple inhertance combined with trampolines. The next code explains the problem: #include #include namespace py = pybind11; // classes class A { public: virtual int fA() const =…
Naum
  • 105
  • 1
  • 7
1
vote
2 answers

does the move constructor invalidate shared_from_this

I would like to start a thread with a class which contains non-copyable members. In order to communicate with the thread I would like to create a shared pointer from the object before I move it into the thread. Does the move constructor invalidate…
0
votes
0 answers

Why is the weak reference count of a std::shared_ptr not zero after std::make_shared invoked?

After executing lines 40 and 41 below, the debugger is telling me that the weak reference count for the std::shared_ptr's a and b is 2. The ctor for Value is not storing any strong or weak references to these objects: struct Value : public…
wcochran
  • 10,089
  • 6
  • 61
  • 69
0
votes
2 answers

'std::bad_weak_ptr' error while using shared_from_this

Note: Before posting the question, I have gone through the existing questions on std::bad_weak_error while using shared_from_this to pass the shared_ptr of the existing shared_ptr instance to another method. None of them are similar to this…
LKB
  • 127
  • 2
  • 8
0
votes
0 answers

Signals2 disconnects slot automatically?

Code below is a test program, emulating some events processing. Full code snippets is here. There are 3 threads for event producer, for event distributor like event bus, what handles system event too, and for a certain game, what handles significant…
0
votes
1 answer

Why doesn’t `std::make_shared` work anymore when the class inherits class `enable_shared_from_this`?

Why doesn’t std::make_shared work anymore when the class Widget inherits class enable_shared_from_this? Here is the code snippet: #include #include #include class Widget:public…
John
  • 2,963
  • 11
  • 33
0
votes
2 answers

smart pointer as argument of function which uses std::function, std::bind

#include #include #include template struct Message { T a; }; template class Connection : public std::enable_shared_from_this { typedef std::function&)>…