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

Ref counted smart pointer's assignment operator

despite the ocean of smart pointer questions out there, I seem to be stuck with one more. I am trying to implement a ref counted smart pointer, but when I try it in the following case, the ref count is wrong. The comments are what I think should be…
James Kuang
  • 10,710
  • 4
  • 28
  • 38
2
votes
3 answers

C++ shared_ptr based singletone what causes link error?

So I try this code: #ifndef TRANSMITTER_H #define TRANSMITTER_H class connector { public: static boost::shared_ptr Instance(){ if(!instance) { instance = boost::shared_ptr(new connector()); …
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
2
votes
1 answer

How do I extend C++ boost list container to implement a thread safe implementation using boost upgrade mutex?

I wrote some sample test code to verify the functionality of using boost upgrade mutexes to implement a read/write mutex lock over a boost list container. I have ten threads, 5 are readers, 5 are writers. I used smart pointers to simplify memory…
2
votes
2 answers

Returning a shared_ptr from a function

I'm very new to C++11, 'still very much experimenting with the extensions. I find the auto keyword very convenient, particularly when dealing with template variables. This means that given template struct…
Olumide
  • 5,397
  • 10
  • 55
  • 104
2
votes
1 answer

Proper use of boost::shared_ptr in a list

I have a question related to boost::shared_ptr<> in C++. I am currently willing to perform a smart deletion of the items of my list: If the item is in use, don't do anything, and delete it later If the item is not in use, delete it This is a…
Rippalka
  • 380
  • 6
  • 16
2
votes
1 answer

C++ template : A template to call variables by name

This question is a continuation of an earlier one found at: How to template'ize variable NAMES, not types? Let's say one has the following code: struct VAR_TYPE{ public: bool is_fixed; double value; // Numerical value std::string…
user1628622
2
votes
5 answers

Inside the copy constructor of shared_ptr

I have some confusion about the shared_ptr copy constructor. Please consider the following 2 lines: It is a "constant" reference to a shared_ptr object, that is passed to the copy constructor so that another shared_ptr object is initialized. The…
softwarelover
  • 1,009
  • 1
  • 10
  • 22
2
votes
1 answer

static_pointer_cast resulting in a shared_ptr that points to a different location

I have made a World class to store a list of derived Object. I have ended up with the below: class World { typedef std::shared_ptr object_ptr; public: template void registerObject(T& object) { auto sp =…
Lerp
  • 2,957
  • 3
  • 24
  • 43
2
votes
2 answers

C++ boost::shared_ptr as a member of a class and repeatedly setting its value

I am trying to figure out the cleanest way to implement using a boost::shared_ptr as a member of a class. Let's say I have class A that contains a boost::shared_ptr to a class B member. The B class could be either the base type (B) or a derived type…
user1040229
  • 491
  • 6
  • 18
2
votes
2 answers

Are pointers to memory copied during data transfer to/from function return values/arguments and class requests?

Let's say I have the following function (where mVertexShader is an std::shared_ptr< ID3D11VertexShader > class member, and mRenderData is just POD holding other D3D stuff): void VertexShader::CreateShader( void ) { GVertexShader*…
zeboidlund
  • 9,731
  • 31
  • 118
  • 180
2
votes
2 answers

iterate over a map of shared pointers without increasing the reference count?

I would like to know whether the following code will result in an increase in the reference count for each shared pointer, or whether the optimizer will be clever enough to recognize that we are not in fact copying the pointer, just dereferencing…
Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
2
votes
2 answers

Why does boost::shared_ptr use gcc inline assembly to increase use_count instead of using operator++?

I'm reading boost::shared_ptr source code and found it using this function to increase shared_ptr's use count(reference count): inline void atomic_increment( int * pw ) { //atomic_exchange_and_add( pw, 1 ); __asm__ ( "lock\n\t" …
amazingjxq
  • 4,487
  • 7
  • 33
  • 35
2
votes
2 answers

boost::shared_ptr and multithreaded access

I'm trying to implement a multithreaded framework, in which output objects are created at the end of every frame that my networking thread runs, so that another thread can, at the beginning of its frame, obtain the most recent "completed output"…
Nick Johnson
2
votes
2 answers

How to fix: error: invalid conversion from 'const MyClass*' to 'MyClass*'

I am getting this compile error: error: invalid conversion from 'const MyClass*' to 'MyClass*' Here is the code: std::tr1::shared_ptr myClassA; const MyClass* myClassB; myClassA = std::tr1::shared_ptr(myClassB); // error here I…
kanso
  • 750
  • 8
  • 17
2
votes
3 answers

Segmentation fault in destructor of own shared pointer

For training purposes, I am trying to write my own smartpointer, imitating std::shared_ptr. I have a static std::map ref_track that keeps track whether there are still shared pointer referencing a certain block in memory. My concept is…
nikolas
  • 8,707
  • 9
  • 50
  • 70
1 2 3
99
100