Questions tagged [shared-ptr]

Reference counted smart pointer class implementing shared ownership

A shared_ptr is a non-intrusive smart pointer that manages the ownership of a shared resource. Multiple shared_ptr objects can share ownership of the same resource, which will be destroyed automatically when there are no more "non-empty" shared_ptr objects referring to it.

The different versions and implementation commonly used are boost::shared_ptr (the original), std::shared_ptr (included in the C++11 standard) and std::tr1::shared_ptr (from the TR1 report on library extensions, lacks certain features such as aliasing).

The related class template weak_ptr is designed for use with shared_ptr and represents a non-owning reference to a resource that is managed by a shared_ptr. This useful to avoid circular references. A weak_ptr can also be used as a "tracking reference" and converted to a shared_ptr to temporarily assume shared ownership when it is required.

3323 questions
16
votes
5 answers

Is it safe to use STL (TR1) shared_ptr's between modules (exes and dlls)

I know that new-ing something in one module and delete-ing it in another can often cause problems in VC++. Problems with different runtimes. Mixing modules with staticly linked runtimes and/or dynamically linked versioning mismatches both can screw…
Aardvark
  • 8,474
  • 7
  • 46
  • 64
16
votes
3 answers

How to use a shared_ptr with a pointer to struct that should not be freed

Currently I'm using some functions from the glib library. With glib also comes the gio. glib is a C library and therefore I need to delete some structures that I create. for many of the objects I create a smartpointer…
hetepeperfan
  • 4,292
  • 1
  • 29
  • 47
16
votes
2 answers

How to initialize a shared pointer in the initialization list of a constructor?

How can I initialize a shared pointer in the initialization list of a constructor? I have this: Foo::Foo (const callback &cb) { Bar bar; bar.m_callback = cb; m_ptr = std::make_shared(bar); //... } I would like to put this into…
waas1919
  • 2,365
  • 7
  • 44
  • 76
16
votes
9 answers

std::auto_ptr or boost::shared_ptr for pImpl idiom?

When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of a std::auto_ptr? I'm sure I once read that the boost version is more exception friendly? class Foo { public: Foo(); private: struct impl; …
Rob
  • 76,700
  • 56
  • 158
  • 197
16
votes
3 answers

Getting shared_ptr refs to appear in doxygen collaboration diagrams

I've done enough Googling to know that if I have something like class SubObject { public: //blah blah blah }; class Aggregate { public: boost::shared_ptr m_ptr; }; I can get Doxygen to create the "correct" collaboration diagram if…
Eric H.
  • 2,566
  • 2
  • 23
  • 34
16
votes
2 answers

C++11 storing multiple shared pointers as raw pointers

My question concerns shared_ptr and make_shared in C++11. I have two vectors, the first one stores smart pointers and the second one stores raw pointers. The first vector works as I had expepted but vector2 is just confusing... Code sample #include…
user2442944
16
votes
3 answers

How to convert an object instance to shared_ptr instance

Suppose I had two shared_ptr types such as boost::shared_ptr sptrA; boost::shared_ptr sptrB; Now suppose that sptrA->SomeMethod() returned a simple ObjB type (not a shared ptr). Is it possible for me to store that type somehow in sptrB…
Casper_2211
  • 1,075
  • 5
  • 14
  • 25
16
votes
1 answer

false sharing in boost::detail::spinlock_pool?

I came across this SO question and reading it over eventually led me to look at boost::detail::spinlock_pool. The purpose of boost::detail::spinlock_pool is to reduce potential contention for a global spinlock by choosing from an array of spinlocks…
mcmcc
  • 822
  • 6
  • 12
16
votes
3 answers

Should I std::move a shared_ptr in a move constructor?

Consider: #include #include #include #include #include #include using namespace std; class Gizmo { public: Gizmo() : foo_(shared_ptr(new string("bar"))) {}; Gizmo(Gizmo&&…
John Dibling
  • 99,718
  • 31
  • 186
  • 324
15
votes
3 answers

Does it make sense to check for nullptr in custom deleter of shared_ptr?

I've seen some code that uses std::shared_ptr with a custom deleter that test the argument for nullptr, for example, MyClass which has a close() method and is constructed with some CreateMyClass: auto pMyClass =…
ZivS
  • 2,094
  • 2
  • 27
  • 48
15
votes
1 answer

std::shared_ptr and Inheritance

I am having some problems with automatic typecasting between shared_ptr of inherited classes. My class structure is as follows, a base class Base and two derived classes Derived1 and Derived2. // Base class class Base { protected: ... …
ssb
  • 7,422
  • 10
  • 36
  • 61
15
votes
4 answers

What is the cyclic dependency issue with shared_ptr?

I read about shared pointers and understood how to use. But I never understood the cyclic dependency problem with shared pointers and how weak pointers are going to fix those issues. Can any one please explain this problem clearly?
kadina
  • 5,042
  • 4
  • 42
  • 83
15
votes
2 answers

Why std::shared_ptr calls destructors from base and derived classes, where delete calls only destructor from base class?

Why when using std::shared_ptr deallocation calls destructors from both base and derived classes when second example calls only destructor from base class? class Base { public: ~Base() { std::cout << "Base destructor" <<…
Piotr Wach
  • 913
  • 2
  • 9
  • 22
15
votes
4 answers

C++11: extending std::is_pointer to std::shared_ptr

I think about overloading std::is_pointer in C++11 to yield true for std::shared_ptr as well, since the latter behaves very much as a T*. #include namespace std { template struct is_pointer> :…
Erwin411
  • 734
  • 1
  • 7
  • 14
15
votes
2 answers

How does a weak_ptr know that the shared resources has expired?

Considering the following code: #include #include using namespace std; struct MySharedStruct { int i; }; void print_value_of_i(weak_ptr weakPtr) { if (shared_ptr sp = weakPtr.lock()) {…
Samaursa
  • 16,527
  • 21
  • 89
  • 160