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

boost::shared_ptr and Return Type Resolver idiom

I am currently working on a concept of Object known in Java or C# for C++. It would be similar to variant type like boost::any, however having wider functionality. For that purpose I am using boost::shared_ptr to internaly store actual data and I…
mati
  • 39
  • 2
2
votes
2 answers

c++ vector of boost::shared_ptr

I just started learning boost shared pointers. I wrote a short program, results look good but I'm not sure if memory is deallocating well with my code. I would like to ask, if someone could look at my code and tell if I correctly use shared…
ST3
  • 8,826
  • 3
  • 68
  • 92
2
votes
1 answer

C++11 Composite Pattern with Smart Pointers

I am working on a personal project to familiarize myself with C++11 and Boost. I have a inheritance relationship with a UrlExtractor base class, with a TagUrlExtractor derived class and a CompoundExtractor derived class. A CompoundExtractor can…
Travis Parks
  • 8,435
  • 12
  • 52
  • 85
2
votes
3 answers

Boost shared_ptr passing a derived class

So I have something like so class baseclass { .... } class derived : public baseclass { ... } void func(boost::shared_ptr& a){ //stuff } boost::shared_ptr foo; func(foo); Will this work? I assume not because its not the same…
csteifel
  • 2,854
  • 6
  • 35
  • 61
2
votes
1 answer

Compiler-specific error: can't match function with const arguments

I'm pretty new to C++, so I'm trying to figure out exactly what's going on here. I'm trying to make (someone else's) code compile. It runs fine using mingw, but I'm also crosscompiling onto an embedded system (TS-7800) and running into compiler…
chessbot
  • 436
  • 2
  • 11
2
votes
1 answer

Using std::shared_ptr to share data between producer/consumer threads

I am trying to use std::shared_ptr to point to the data being produced by one thread and consumed by another. The storage field is a shared pointer to the base class, Here's the simplest Google Test I could create that reproduced the…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
2
votes
3 answers

Shared pointers and raw pointers in same container

I need to populate container with shared pointers and raw pointers at same time. I guess shared_ptr may be forced to behave like T*, if constructed with no-op deleter and no-op (de)allocator? Or may be there is universal smart pointer, which…
Shadows In Rain
  • 1,140
  • 12
  • 28
2
votes
1 answer

Copy-on-write pointer object in C++

I tried to follow this article http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-on-write on how to implement copy on write pointers in C++. The problem is, it doesn't work for me. The crux of the object is in overloading the dereference…
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
2
votes
1 answer

Global smart pointer is not cleaning up properly

I have a c++ interface, and the derived class of that interface in one DLL, I am using the class in another process by including the interface header file, and importing a factory function that returns an object of the derived class (COM…
aiman09
  • 105
  • 8
2
votes
2 answers

Why does enable_shared_from_this lack direct access to the embedded weak_ptr?

I want to use boost signals2 with automatic connection management in a multithreaded application. My class inherits from enable_shared_from_this<> and i want to connect a member method from within another member method. The connection might be…
eel76
  • 97
  • 12
2
votes
1 answer

shared_ptr not reporting referenced object deletion

I'm running this code in MS Visual Studio 10, #include #include using namespace std; class A { int i; public: A(int j) : i(j) {} ~A() {} void fun() { cout << "A::i = " << i << endl; } }; int…
masT
  • 804
  • 4
  • 14
  • 29
2
votes
1 answer

Is there a better way of allocating/copying shared_array

I have a stream object which provide GetBuffer() and GetBufferSize() methods. The GetBuffer method returns a raw uint8_t pointer. I want to pass (by value) this buffer to another object which expects a shared_array. I'm using boost, (which…
benf
  • 915
  • 1
  • 9
  • 28
2
votes
3 answers

GCC shared_ptr Template Error

The following function #include template std::shared_ptr Tail(const std::shared_ptr& cont, size_t n) { const auto size(std::min(n, cont->size())); return std::shared_ptr(new…
Craig
  • 413
  • 7
  • 16