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

c++11 - Ownership and getters

I'm new to C++ and I have troubles wrapping my head around ownership, specifically with a getter. Here's some example code: class GameObject { public: Transform *transform(); private: Transform _transform; }; I guess a raw pointer is unsafe to…
cboe
  • 469
  • 1
  • 9
  • 25
13
votes
4 answers

Forward declarations and shared_ptr

I'm trying to refactor my code so that I use forward declarations instead of including lots of headers. I'm new to this and have a question regarding boost::shared_ptr. Say I have the following interface: #ifndef I_STARTER_H_ #define…
Baz
  • 12,713
  • 38
  • 145
  • 268
13
votes
2 answers

How to combine the use of std::bind with std::shared_ptr

I need to do something like this more than often: AsyncOperation * pAsyncOperation = new AsyncOperation(); auto bindOperation = std::bind(&AsyncOperation::operator(), std::ref(*pAsyncOperation)); std::thread thread(bindOperation…
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
12
votes
2 answers

Using std::shared_ptr with clang++ and libstdc++

I'm trying to use the std::shared_ptr in clang++(clang version 3.1 (trunk 143100)) using libstdc++(4.6.1). I have a little demo program: #include int main() { std::shared_ptr some(new int); std::shared_ptr other(some); …
Bjorn
  • 941
  • 9
  • 18
12
votes
2 answers

Passing a shared pointer by reference or by value as parameter to a class

Should a shared pointer be passed by reference or by value as a parameter to a class if it is going to be copied to a member variable? The copying of the shared pointer will increment the refrence count and I don't want to make any unnecessary…
Signekatt
  • 123
  • 1
  • 5
12
votes
2 answers

Using smart pointers for C++ binary search tree

I have implemented a binary search tree in c++. Instead of using bare pointers to point to a nodes children I used the std::shared_ptr. The nodes of the tree are implemented as follows struct treeNode; typedef std::shared_ptr
dayman
  • 123
  • 1
  • 5
12
votes
1 answer

How to test whether a shared_ptr is empty or owns nothing

A C++ std::shared_ptr<..> may be empty and it may also be null. Both of these concepts exist and they are not equivalent. Additionally, neither implication is always true between these cases. The latter case is trivial to detect because operator…
Xharlie
  • 2,350
  • 3
  • 19
  • 39
12
votes
1 answer

Do boost::shared_ptr and boost::shared_ptr share the reference count?

There are several interesting questions on pitfalls with boost::shared_ptrs. In one of them, there is the useful tip to avoid pointing boost::shared_ptr and boost::shared_ptr to the same object of type Derived since they use different…
lytenyn
  • 819
  • 5
  • 21
12
votes
10 answers

Using boost::shared_ptr in a library's public interface

We have a C++ library that we provide to several different clients. Recently we made the switch from using raw pointers in the public interface to using boost::sharedptr instead. This has provided an enormous benefit, as you might guess, in that now…
Brian Stewart
  • 9,157
  • 11
  • 54
  • 66
12
votes
2 answers

Is an empty aliasing shared_ptr a good alternative to a no-op deleting shared_ptr?

Sometimes I need shared_ptr instances that have a no-op deleter, because an API expects a shared_ptr instance that it wants to store for a limited time but I am given a raw pointer that I am not allowed to own for a time larger than what I am…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
12
votes
3 answers

extending shared_ptr by inheritance

What is so bad about doing something like this? class myclass : public std::shared_ptr { // some code, an allocation is never done std::string get_info () { if(*this != nullptr) return "" + (this->info * 3) +…
ribamar
  • 1,435
  • 1
  • 16
  • 26
12
votes
4 answers

Vector construction when wrapped in a Shared Pointer

So I am working on a transformation from an OO-language with garbage collection capabilities to C++. To start out I want to wrap all objects in shared pointers to solve the memory de-allocation issue. Right now I am trying to wrap a vector in a…
Flipbed
  • 719
  • 9
  • 18
12
votes
3 answers

How to access target of std::tr1::shared_ptr in GDB

How can I access target of a std::tr1::shared_ptr in GDB. This doesn't work: (gdb) p sharedPtr->variableOfTarget If I try with the pointer object itself (p sharedPtr) I get something like this: $1 = std::tr1::shared_ptr (count 2) 0x13c2060 With a…
Pnog is not Pong
  • 175
  • 1
  • 1
  • 8
12
votes
2 answers

Unable to use custom allocator with allocate_shared/make_shared

In my C++11 program, I use shared_ptr for some objects which are actively created and deleted. It so happened that standard allocator with operator new is a bottleneck, so I want to create my own one, which will allocate a bunch of memory at once…
yeputons
  • 8,478
  • 34
  • 67