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
0 answers

Give a default value for shared pointer argument

The following code gives a compilation error in VS2010, which support shared_ptr and make_shared function. Why and how to correct it? #include class A { A(std::shared_ptr p = std::make_shared()) // error is at this line …
user1899020
  • 13,167
  • 21
  • 79
  • 154
2
votes
0 answers

Serialization of boost::shared_ptr through a custom archive

I'm trying to serialize a boost::shared_ptr to a custom archive. The main problem I'm facing is that the boost::serialization code for shared_ptr requires the archive to have both a 'reset' and an 'append' method. Does anyone know where these…
Silviu P
  • 41
  • 2
2
votes
2 answers

Which type of pointer to use to implement shared access to elements of a set?

In order to make the discussion clear, I'm going to describe the problem in a very general manner, i.e. I will neither provide names of real classes nor will I describe the domain/context (however, I might if it turns out to be urgent). Imagine…
Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85
2
votes
3 answers

vector of shared_ptrs, returning it from a function and modifying it

Essentially, I want 2 of my classes to be sharing some data, which i want to have as a vector of shared_ptr objects. I have cut down my code into the following simple compilable example. I want object A, to be looking at the data that was…
CptLightning
  • 555
  • 1
  • 8
  • 18
2
votes
3 answers

C++11 cast const iterator pointing to container of shared_ptr objects

I have an STL container whose element type is const std::shared_ptr. I want to supply two iterator types to the user: MyContainer::iterator typedefed as std::vector>::iterator (which should be the same type…
2
votes
1 answer

boost tcp socket with shared_ptr c++

I am trying to wrap the boost TCP using a new class in c++. Things work like a charm while I call the boost function directly. However I fail to call socket close while the close is wrap in a class function. Please help have a look on the following…
HenryLok
  • 81
  • 1
  • 7
2
votes
1 answer

std::function for method argument, copy or shared_ptr?

I'm attempting to use std::function as a callback handler. Such like this, void some_job( int arg1, std::function & callback ) { callback( process(arg1) ); } In this example i used pass-by-reference. But the problem is, I can't…
user2020192
  • 109
  • 1
  • 6
2
votes
0 answers

intrusive_ptr, shared_ptr performance tests

class X { public: std::string name; int age; long references; X(string n, int a) : references(0), name(n), age(a) {} }; inline void intrusive_ptr_add_ref(X* x){ ++x->references; } inline void intrusive_ptr_release(X* x){ if(--x->references ==…
Michael
  • 673
  • 2
  • 5
  • 23
2
votes
1 answer

C++ hidden constructor for objects managed by shared_ptr

I have a class which inherits from enable_shared_from_this. It has shared_ptrs to child objects and there's one "root" object, so the whole hierarchy is managed by shared_ptr. This way an object can have multiple parents and be destructed safely. I…
2
votes
1 answer

When making an instance with shared_ptr, what should happen with the pointer instance variables?

Alright, here I have this small example of my complex class class LivingObject { Ogre::SceneNode* myNode; Gorilla::ScreenRenderable* myScrRend; Gorilla::Layer* myRendLayer; Gorilla::Rectangle* myRendRect; int…
Molma
  • 171
  • 9
2
votes
1 answer

C++ Design: Passing pointer/reference to ref-counted object

I have a directed acyclic graph, composed of Node objects. Each node has a list of std::shared_ptrs to other nodes, which are its children in the graph. I have lots of useful methods I need, such as inserting/emplacing/reparenting nodes, testing if…
2
votes
1 answer

How to append raw data to boost::shared_array?

I had: message->data = boost::shared_array(new char[100]); And it was filled with data. I received new char * data (from old C API) of int length. I want to expand my array with new data meaning write a copy of that new data after the end of…
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
2
votes
2 answers

Deleting content of a boost::shared_ptr

I have a doubt with boost::shared_ptr. I have seen this in a destructor (with ptr being a boost::shared_ptr): ptr = boost::shared_ptr< int >( new int ). Is that ok?. Shouldn't it be ptr.reset(), like stated here: How to intentionally delete a…
Adri C.S.
  • 2,909
  • 5
  • 36
  • 63
2
votes
1 answer

Uncaught exception at constructor after allocating memory

I've read that awesome summary of Michael Burr regarding a constructor that throws an exception, here: Will the below code cause memory leak in c++ My question is: Is the behavior similar when an exception is thrown by a function called from…
Subway
  • 5,286
  • 11
  • 48
  • 59
2
votes
2 answers

Identifying which Base Class shared_ptr has been passed into a Super Class shared_ptr vector

I am working on a C++ project, specifically implementing a shunting yard algorithm. I have a function that creates a vector of shared_ptr's of type super class, but the classes that are being pushed into this vector are all base class shared_ptrs.…
mcraenich
  • 745
  • 1
  • 14
  • 38
1 2 3
99
100