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
31
votes
5 answers

Detach a pointer from a shared_ptr?

Possible Duplicate: How to release pointer from boost::shared_ptr? A function of my interface returns a pointer to an object. The user is supposed to take ownership of that object. I do not want to return a Boost.shared_ptr, because I do not want…
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
31
votes
1 answer

How to avoid memory leak with shared_ptr?

Consider the following code. using boost::shared_ptr; struct B; struct A{ ~A() { std::cout << "~A" << std::endl; } shared_ptr b; }; struct B { ~B() { std::cout << "~B" << std::endl; } shared_ptr a; }; int main() { …
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
31
votes
4 answers

In gdb, I can call some class functions, but others "cannot be resolved". Why?

I have not worked on shared pointers yet .. I just know the concept. I'm trying to debug functions in the following c++ class, which stores data of an XML file (read-in via the xerces library). // header file class ParamNode; typedef…
Sebastian
  • 1,408
  • 1
  • 20
  • 28
30
votes
4 answers

Create a boost::shared_ptr to an existing variable

I have an existing variable, e.g. int a = 3; How can I now create a boost::shared_ptr to a? For example: boost::shared_ptr< int > a_ptr = &a; // this doesn't work
Bill Cheatham
  • 11,396
  • 17
  • 69
  • 104
30
votes
4 answers

Why is the size of make_shared two pointers?

As illustrated in the code here, the size of the object returned from make_shared is two pointers. However, why doesn't make_shared work like the following (assume T is the type we're making a shared pointer to): The result of make_shared is one…
Clinton
  • 22,361
  • 15
  • 67
  • 163
30
votes
2 answers

why i can't cast nullptr to weak_ptr<>

class MyClass { public: MyClass(std::weak_ptr parent){} } i want to do this: auto newInstance = std::make_shared(nullptr); or default value of weak_ptr argument is null, such as : void function(int arg,std::weak_ptr
uray
  • 11,254
  • 13
  • 54
  • 74
30
votes
2 answers

Does the standard behavior for deleters differ between shared_ptr and unique_ptr in the case of null pointers?

OK, so first some things that might be relevant: I'm using the Clang 3.1 compiler, in C++11 mode, with the standard library set to libc++. I'm trying to familiarize myself with C++11, and in so doing I ran across behavior that seems odd. It may be a…
Bob Miller
  • 603
  • 5
  • 12
29
votes
4 answers

boost Shared_pointer NULL

I'm using reset() as a default value for my shared_pointer (equivalent to a NULL). But how do I check if the shared_pointer is NULL? Will this return the right value ? boost::shared_ptr blah; blah.reset() if (blah == NULL) { //Does this…
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
29
votes
1 answer

enable_shared_from_this and inheritance

I've got a type which inherits from enable_shared_from_this, and another type that inherits from this type. Now I can't use the shared_from_this method because it returns the base type and in a specific derived class method I need the derived…
Puppy
  • 144,682
  • 38
  • 256
  • 465
29
votes
1 answer

Why does libc++'s implementation of shared_ptr use full memory barriers instead of relaxed?

In boost's implementation of shared_ptr, it uses relaxed memory ordering to increment its reference count. This appears safe as decrements use acquire/release to make sure that any previous decrements are visible to the thread before releasing…
Christopher Tarquini
  • 11,176
  • 16
  • 55
  • 73
29
votes
5 answers

Should I use shared_ptr or unique_ptr?

I have a question about std::unique_ptr and std::shared_ptr. I know there are loads of questions about when to use which one, but I'm still not sure if I understand it correctly. I read somewhere that the default choice for smart pointer should be…
dziwna
  • 1,212
  • 2
  • 14
  • 25
29
votes
3 answers

std::shared_ptr and initializer lists

The std::shared_ptr constructor isn't behaving as I expected: #include #include void func(std::vector strings) { for (auto const& string : strings) { std::cout << string << '\n'; } } struct…
dpj
  • 933
  • 1
  • 7
  • 14
28
votes
6 answers

Can you use a shared_ptr for RAII of C-style arrays?

I'm working on a section of code that has many possible failure points which cause it to exit the function early. The libraries I'm interacting with require that C-style arrays be passed to the functions. So, instead of calling delete on the…
bsruth
  • 5,372
  • 6
  • 35
  • 44
28
votes
2 answers

Move a unique_ptr with custom deleter to a shared_ptr

I have a function which creates a unique_ptr with a custom deleter and returns it: auto give_unique_ptr() { auto deleter = [](int* pi) { delete pi; }; int* i = new int{1234}; return std::unique_ptr(i,…
j00hi
  • 5,420
  • 3
  • 45
  • 82
28
votes
1 answer

Deleter type in unique_ptr vs. shared_ptr

I thought it is very curious when I discovered that the standard defines std::unique_ptr and std::shared_ptr in two totally different ways regarding a Deleter that the pointer may own. Here is the declaration from cppreference::unique_ptr and…
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64