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.

2992 questions
1
vote
1 answer

How to use std::transform and std::mem_fn correctly?

I have implemented a C++ program, which creates three different Person objects. These objects are made shared and stored in a vector. The function getVector() takes a const std::vector>& as input and returns a…
Rainer H.
  • 55
  • 6
1
vote
1 answer

Shared pointer to incomplete needs deleter in reset method

I am working with shared_ptr storing pointers of a C library. Here an example of such a C library containing the header bar.h: #pragma once typedef struct Flupp MyFlupp; MyFlupp * create_flupp(); void del_flupp(MyFlupp *…
1
vote
1 answer

Shared pointer lifetime

If I dereference a shared_ptr and invoke a method on the contained object, is the shared_ptr lifetime guaranteed? Let's say: stream.linkInfoPtr->addTxRxBytes( txBytes, rxBytes ); Where linkInfoPtr is shared_ptr and stored in the stream object. Does…
teoring
  • 85
  • 12
1
vote
0 answers

Is there a way of replacing object pointed to by a shared_ptr without changing the value of the pointer itself?

In my code I have an application which contains a state manager and a list of the applications possible states. When creating the different states I want to pass to it a list of commands (following the command pattern) that, when executed, changes…
Viktor Axén
  • 169
  • 1
  • 10
1
vote
2 answers

Unexpected invocation of the destructor after an invokation of a product of std::bind

There is a simple example with making use of boost::asio::io_context https://github.com/unegare/boost-ex/blob/500e46f4d3b41e2abe48e2deccfab39d44ae94e0/main.cpp #include #include #include…
dronte7
  • 792
  • 2
  • 14
1
vote
1 answer

Usage of shared_ptr between multiples threads / functions - IDS Peak API

I am facing an issue with a share_ptr between 2 functions (main and svr.Get(...) of HTTPlib library). I'm declaring my pointer using : std::shared_ptr dataStream; I'm initializing it using on the main : dataStream =…
Spawnrider
  • 1,619
  • 1
  • 19
  • 30
1
vote
3 answers

Does resetting std::shared_ptr lead to resetting its deleter?

maybe I misunderstood some aspects of smart pointers in c++, but after executing this code: class cls { public: class deleter { public: const cls& obj; deleter(const cls& c) : obj(c) {} void operator()(int* num) …
Nikita128
  • 67
  • 1
  • 7
1
vote
1 answer

How to controll shared ptr reference count?

I'm creating a Resource Manager for my game engine. Basically it have an unordered_map that stores the path of the resource as key and as value it stores the loaded resource. This is the code responsible for loading assets. std::shared_ptr
Brian Batista
  • 67
  • 1
  • 6
1
vote
1 answer

std::shared_ptr with std::function as custom deleter and allocator

Is there a way to make this work? #include #include int main() { std::function allocator = [](size_t size){ return new unsigned char[size]; }; std::function
user9400869
1
vote
2 answers

moving smart pointers twice vs copying

Is there any significant difference in performance, memory, etc, between: #1: moving a pointer to a temporary pointer, moving it back, then deleting the temporary pointer #2: copying a pointer to a temporary pointer, then deleting the temporary…
AlexG
  • 4,451
  • 5
  • 21
  • 43
1
vote
2 answers

C++ std::shared_ptr, is operator= overloaded to use copy constructor?

If I do the following: class Foo { std::shared_ptr bar; Foo(std::shared_ptr bar) { this->bar = bar; } } std::shared_ptr func() { std::shared_ptr bar_ptr(new Bar()); std::shared_ptr
Alexis Winters
  • 409
  • 3
  • 12
1
vote
1 answer

Boost serialization (bug?) issue with MSVC, with vector in several libraries

We use boost serialization in ours projects, both on linux (gcc) and Windows. We encounter a problem only on Windows (for "every" versions of Windows (7 and 10) and of MSVC: Visual 2010 and 2015, ie. MSVC++ 10.0 _MSC_VER == 1600 or MSVC++ 14.0 …
Nico
  • 184
  • 10
1
vote
1 answer

Downcasting from shared pointer of base class to reference of derived class

I'm learning to use shared pointers. Lets say we have two classes, Base and Derived (inheriting from Base), without polymorphism (no virtual method, except destructor), and a loop : for (std::shared_ptr& pB : collection) {
A. Gille
  • 726
  • 3
  • 17
1
vote
1 answer

JNI how to manage the life cycle of a c++ buffer

I have an image buffer allocated on the heap in my c++ code that I would like to share with some other c++ objects as well as Java objects through JNI. On the native side im using shared_ptr and I was wondering what is the best way to do so ? my…
1
vote
1 answer

Fix memory leaks in my own doubly-linked-list

I am trying to write my own doubly linked list. But Valgrind says that I have memory leaks here. I have no any idea what bad can happens in the lines which Valgrind show to me. Could you help me? I've tried to change all my pointer to shared_ptr…
1 2 3
99
100