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
2
votes
2 answers

Passing 2 pointers to 2 threads but they end up sharing the same

I guess this problem has appeared already and it surely shows my beginner level in the world of threads, but I haven't been able to find any previous question nor other resource addressing it. I've gone through the most common introductions to C++11…
Alberto Santini
  • 6,425
  • 1
  • 26
  • 37
2
votes
1 answer

common way to encapsulate C-type pointers with custom deleters

Almost all C-type objects from C-libraries have some custom deleters, e.g. IplImage* from OpenCV has the cvReleaseImage(IplImage**) deleter function. In C++, I want my code in a way that it always ensures that every object gets finally deleted. I…
Albert
  • 65,406
  • 61
  • 242
  • 386
2
votes
1 answer

gcc 4.7.3 internal compiler error when using make_shared with a constructor

I understand where the problem is, I'm just not sure why am I not getting any error output from gcc. The lines in question which generate the issue are: std::string type,rel,pred; std::tie( type, rel, pred ) = tuple; auto supertype =…
Ælex
  • 14,432
  • 20
  • 88
  • 129
2
votes
1 answer

Stealing bits from a std::shared_ptr

I am currently working on a class assignment where I need to implement a lock-free linked list. The structure of each of my nodes is essentially: class Node { std::shared_ptr next; long long key; } I need to somehow embed…
Ken P
  • 552
  • 3
  • 11
2
votes
1 answer

is there a reason why std::make_shared would require a default constructor?

I'm trying to figure out if this is a requirement from cereal or not. I keep getting errors that class Constructors (default ones) are private, which I've put there for a reason. However, the originating line for the error, seems to be,…
Ælex
  • 14,432
  • 20
  • 88
  • 129
2
votes
3 answers

Dereference a shared_ptr returned from a function

I have a class, DevicePointer, that encapsulates a std::shared_ptr. Various classes that need to hold a pointer to a Device derive from DevicePointer. Before I started using shared_ptr, DevicePointer had an ::Expose() function that would…
Robinson
  • 9,666
  • 16
  • 71
  • 115
2
votes
0 answers

boost::variant< boost::shared_ptr > won't compile

The following code fails to compile with g++ 4.7.3 when the -std=c++11 flag is set (error message pasted at the bottom): #include #include int main() { boost::shared_ptr i; boost::variant<…
2
votes
1 answer

how shared_ptr access T's element or function

I don't think I have mastered shared_ptr. Example code: shared_ptr logger; int main(){ logger = make_shared(new ofstream("ttt.txt")); *logger <<"s"; return 0; } Error 1 error C2664:…
Chris Su
  • 543
  • 8
  • 16
2
votes
1 answer

Declaring a boost asio socket, acceptor and endpoint in a class headerfile

I have a TCP/IP server made with boost asio that is wrapped in a class. Now i want declare the socket, eindpoint and acceptor in the class headerfile so that i can make memberfunctions that use the socket. The server runs now in the class…
Roy08
  • 253
  • 2
  • 7
  • 21
2
votes
2 answers

How does enabled_shared_from_this affect the the life time of shared_ptr?

I was looking at one page of the tutorial of boost::asio. class tcp_server { public: tcp_server(boost::asio::io_service& io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 13)) { start_accept(); } private: void…
WiSaGaN
  • 46,887
  • 10
  • 54
  • 88
2
votes
2 answers

shared_ptr, subscription, destructor

I'm using Boost/shared_ptr pointers throughout my application. When the last reference to an object is released, shared_ptr will delete the object for me. The objects in the application subscribes to events in a central location of the application,…
Martin
  • 471
  • 3
  • 6
2
votes
1 answer

What am I doing wrong with std::shared_ptr and std::unique_ptr in my linked list implementation?

I've never used smart pointers before, so I decided to try out implementing a basic little linked list, just to see how it works. The program bellow outputs only the first element of the list, i.e. 5, and then just exits. In the print() function,…
stellarossa
  • 1,730
  • 1
  • 18
  • 29
2
votes
1 answer

Pass C++ object contained in a smart pointer to Python

I have a class in C++. I create an object from this class in my C++ code. I want this object to be accessible in Python. I use boost::shared_ptr to keep the object address. I've checked out some posts about this but wasn't very helpful. I think the…
Novin Shahroudi
  • 620
  • 8
  • 18
2
votes
1 answer

Exception safety C++ shared pointer

I try to implement a JSON framework in C++ and want to make use of polymorphic concepts. I have a class JSONNode which is kind of container that stores other JSONNode objects itself and so on. I am doing this with pointers and dynamic allocation.…
Michbeckable
  • 1,851
  • 1
  • 28
  • 41
2
votes
1 answer

std::shared_ptr passing the deletor

I have this code : // util.h #include template class ArrayDeleter { public: void operator () (T* d) const { delete [] d; } }; std::shared_ptr > V8StringToChar(v8::Handle
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95