Questions tagged [weak-ptr]

std::weak_ptr is a smart pointer that holds a non-owning ("weak") reference to an object that is managed by std::shared_ptr

The std::weak_ptr is a smart pointer that holds a non-owning ("weak") reference to an object that is managed by std::shared_ptr.

The class template std::weak_ptr is designed for use with and represents a non-owning reference; useful in avoiding circular references, it can also be used as a "tracking reference" and converted to a std::shared_ptr when ownership is required.

261 questions
0
votes
1 answer

How to use weak_ptr in tbb::concurrent_unordered_map?

I am using tbb::concurrent_unordered_map to replace std::map in my program like this: Before: class KvSubTable; typedef std::weak_ptr KvSubTableId; std::map > mEntryMap; Now, I use…
Tach
  • 1
  • 1
0
votes
1 answer

std::move return value of weak_ptr::lock messing up reference count of shared_ptr?

i need an explanation of the following behaviour: #include #include #include struct A { std::string s = "foo"; std::weak_ptr h; std::shared_ptr && getR() { return std::move(h.lock()); } …
Jonas Beck
  • 96
  • 7
0
votes
1 answer

Downcasting a shared_ptr to a derived class containing a weak_ptr c++11

I have a segfault occurring when trying to downcast a std::shared_ptr using std::static_pointer_cast, where the derived class also contains a std::weak_ptr. Here is a MWE: #include
0
votes
0 answers

UT friendly singleton - is there a flaw in my reasoning? (2nd attempt)

In my project we have a few singletons, which tends to be problematic in unit tests. So I wanted to find solution for the problem. Here is what I came with so far: smart_singleton.h class smart_singleton { public: static…
Marcin K.
  • 683
  • 1
  • 9
  • 20
0
votes
0 answers

Compile error when using boost::weak_ptr

#include #include using namespace std; using boost::shared_ptr; using boost::weak_ptr; int main() { shared_ptr p(new int(5)); weak_ptr q = p; if(shared_ptr r = q.lock()) { cout << r <<…
ROBOT AI
  • 1,217
  • 3
  • 16
  • 27
0
votes
1 answer

Better shared_ptr by distinct types for "ownership" and "reference"?

I would like to use a pointer-like object Ownership m_foo for the owning object and handle Reference m_someFoo as a classical "pointer" in another context, whereas my Reference should know when the original object does not exist anymore…
markusneg
  • 3
  • 1
0
votes
0 answers

Following code is working fine in iOS devices while crashing on Android devices. Can somebody help me?

typedef std::shared_ptr RObject; class RObjectbase : public std::enable_shared_from_this { public: virtual ~RObjectbase () {} }; class RObject: public RObjectbase { //some functions }; class ABC { public: …
kiri
  • 9
  • 1
0
votes
2 answers

C++ bi-directional association: object access with smart pointers seems to corrupt the instance

I had a bi-directional object association implemented with raw pointers and it worked without flaw. Then I decided to refactor my code with smart pointers and all of a sudden a string member (depName) of one of the classes (Department) is no longer…
Schulefant
  • 13
  • 7
0
votes
2 answers

How to initialize object's manager w/o initializing object in shared_ptr?

I want to use smart pointers in this way: using namespace std; shared_ptr foo; weak_ptr bar (foo); foo = make_shared("some string"); cout << *bar.lock(); // SIGSEGV The clue is how to initialize shared_ptr's object manager…
saleph
  • 149
  • 11
0
votes
1 answer

N-tree, shared_ptr and "cannot declare field "tnode::data" to be of abstract type Figure

I recently began to learn object oriented programming and now I'm faced with the problem of changing program with usual pointers to program with shared_ptr. I try to create a program with a container (n-tree), which contains twi types of figures:…
0
votes
2 answers

c++ classes linked to each other with std safe pointers (c++)

I have problem in my project. I believe shared/weak pointers from new c++ stabdards can solve it but its unclear to me how. In book i read about standard library there is no comment on my idea how to use these pointers. In my application i have…
Croll
  • 3,631
  • 6
  • 30
  • 63
0
votes
2 answers

Shared pointer inside 2 objects connecting each other

I'm trying to design a class to design a 3D mesh in C++. My idea is the following: 3 base classes: Point, Polygon, Polyhedron (the mesh has to handle very general shapes) The class Polygon contains a vector of shared pointers to Point (the…
stefano1
  • 161
  • 10
0
votes
2 answers

Syntax for converting expired weak_ptr to shared_ptr

From what I've read, a shared_ptr does not get de-allocated until both strong references AND weak references to it are dropped. I understand a shared object to be considered expired when there are no more strong references to it. The standard…
Nicholas
  • 1,392
  • 16
  • 38
0
votes
1 answer

Pass weak this pointer to its own function

Is it a good idea to have code like this: Any pitfalls ? Is it a better design to use shared this pointer ? class X { public: void foo(); void bar2(const boost::weak_ptr& x); }; void X::foo() {} void X::bar2(const boost::weak_ptr&…
gudge
  • 1,053
  • 4
  • 18
  • 33
0
votes
2 answers

Binding a callbacks to an expiring shared_ptr?

I am familiar with std::shared_ptr and std::weak_ptr and know how they work. However, I would like the std::shared_ptr to emit a callback, like a boost signal. This would allow std::weak_ptr, who still refer to the deleted object, to be cleaned up…
Jack Sabbath
  • 1,398
  • 2
  • 9
  • 12