Newest 'shared-ptr' Questions - Stack Overflow

Questions tagged [shared-ptr]

Reference counted smart pointer class implementing shared ownership

-1
votes
1answer
36 views

Shared pointer using (this) registering to an observer

I am having a problem trying to use a shared pointer from a class that wants to register to an observer (using ) here is the example. Observer.hpp class Observer { virtual void update() = 0; } ...
-1
votes
2answers
56 views

C++ shared_ptr reference count while binding another smart pointer to pointer returned by get

According to "C++ Primer" page 466, it is an error to bind another smart pointer to the pointer returned by get(). This is followed by some code and further explanation. I am having trouble ...
3
votes
3answers
102 views

Understanding C++ std::shared_ptr when used with temporary objects

I am trying to understand the use of a shared_ptr p when it is used in the construction of an unnamed shared_ptr and the effects this has on p. I was toying with my own examples and wrote the ...
2
votes
4answers
48 views

could there be a racing situation in the following code?

We know the destructor code below is supposed to release the control block if this is the last smart_ptr pointing to the resource being managed. Is it possible that we have a racing problem between ...
-1
votes
1answer
48 views

How to transfer ownership from a vector of shared pointers to another one?

I'm trying to understand what is the best way in C++ to pass data around. In the following example I'm trying to pass ownership of the data (Shader) to a class (ShaderProgram). I could do this with ...
2
votes
1answer
34 views

Understanding enable_shared_from_this<S> from an existing example

I have been trying to understand the usefulness of inheriting from enable_shared_from_this although the working of this mechanism was somewhat explained there. I couldn't find the answer to this ...
1
vote
2answers
68 views

Why does enable_shared_from_this crash if inheritance is not public instead of erroring out

I was using shared_ptr in a project. And at one point I had to store the raw pointers as a void then later convert it back to its shared_ptr form in a callback where the void* was passed. But for some ...
1
vote
2answers
59 views

does the move constructor invalidate shared_from_this

I would like to start a thread with a class which contains non-copyable members. In order to communicate with the thread I would like to create a shared pointer from the object before I move it into ...
2
votes
3answers
116 views

How to interface between a foreign language and a C++ library which returns shared pointers

I'm writing a library which interfaces between kdb+ (although this question is applicable to foreign language interfaces in general), and a C++ library where most of the API calls return a std::...
3
votes
4answers
136 views

Why does std::enable_shared_from_this allow multiple std::shared_ptr instances?

There are several questions that cover the behavior of std::enable_shared_from_this, but I don't think that this is a duplicate. Classes that inherit from std::enable_shared_from_this carry a std::...
-1
votes
0answers
65 views

I don't know why this code makes a segmentation fault

I'm practicing shared_ptr in c++ It makes a segmentation fault but I don't know why if I remove cout line in goo() function, it works well #include <iostream> #include <memory> using ...
1
vote
0answers
69 views

Return a list of non const pointers by const reference to a list of const pointers

I'm facing this problem where one of my objects has a list of shared_ptrs to some object. so the type is like this list<shared_ptr<some_object>>. But the interface I'm implementing demands ...
14
votes
4answers
520 views

Custom allocators as alternatives to vector of smart pointers?

This question is about owning pointers, consuming pointers, smart pointers, vectors, and allocators. I am a little bit lost on my thoughts about code architecture. Furthermore, if this question has ...
1
vote
2answers
107 views

Will full memory barriers around std::shared_ptr's use_count() make it a reliable counter?

I'm implementing a thread-safe "lazy-synchronized" Set as a linked list of nodes connected by shared_ptr's. The algorithm is from "The Art of Multiprocessor Programming". I'm adding an is_empty() ...
0
votes
1answer
55 views

Should I always use shared_ptr to create a class instance(instead of just new)?

As wiki shows, shared_ptr can handle the heap space recycle problems. So, I always use shared_ptr to create instance, is there any problems? and I use it as this, i dont need to delete s, right? s = ...
2
votes
1answer
58 views

How does a lambda that captures a shared_ptr by value affect its use_count()?

I am curious about the lifespan of a shared_ptr when captured by value within a lambda. I had expected its use_count() to always be >= 1 as long as the lambda is still in memory, but my test shows ...
0
votes
2answers
60 views

Assigning a shared_ptr to another after calling get

shared_ptr<std::string> shared_ptr1 = std::make_shared<std::string>("Foo"); shared_ptr<std::string> shared_ptr2 = std::make_shared<std::string>("Bar"); std::string* ...
0
votes
2answers
40 views

Reassigning a pointer when the object it is pointing to is deleted by an instance of another class

I am creating a Tower Defence game mockup where enemies attempt to cross the screen before they are destroyed. Pointers to dynamically allocated Enemy objects are stored in a vector inside an ...
0
votes
0answers
38 views

Is there anyway to enforce friendship within an inheritance tree towards a helper templatized class that encapsulates a static shared_ptr creator?

I'm doing a personal project in which I intend to implement a 3D videogame engine. I'm at the very beginning of it, and I'm trying to define a very basic interface for my window class. In it, I wanted ...
3
votes
2answers
58 views

Lambda function captures wrong “this” pointer

I'm having a problem with making one trivial C++ project involving lambda-expressions and shared pointers to work. The project is in Visual Studio, Debug, x64. This is the code I have. Class1.h: #...
2
votes
3answers
86 views

Can you use a member of deleter to keep alive a shared_ptr?

Given the following types // interface and implementation used in one part of the codebase struct Image { virtual std::vector<uint8_t>& GetData () = 0; }; struct VecImage : public ...
0
votes
1answer
32 views

Uninitialized value when dealing with shared_ptr

I'm sure this is very simple, but I'm rather new to smart pointers, and I couldn't find an answer to this. Scenario is very simple: I have a class A, that holds a shared_ptr to some object X: class ...
1
vote
2answers
73 views

atomic operations on shared pointers, c++ versions

I was working on this thread swap c++ map objects in multithreaded environment However, #include <memory> #include <thread> #include <chrono> #include <atomic> #include <...
2
votes
1answer
96 views

Non-const lvalue reference to type 'A *' cannot bind to a value of unrelated type 'std::shared_ptr<A>'

First of all, there are bunch of similar posts which appear like they are exactly the same issue, but I find that they are different from my problem. I am trying to use smart pointers for the first ...
0
votes
1answer
58 views

Empty weak pointer in enable_shared_from_this

After publicly inheriting enable_shared_from_this and initialzing the object of class, while calling another function of that class, i can still see empty weak pointer of enable_shared_from_this_class ...
0
votes
2answers
28 views

Linked list creating shared_ptr from raw pointer

I am trying to copy my link list starting from the head to a shared_ptr as part of my remove method. For some reason initializing my shared_ptr from a raw pointer is completely deleting my linked list ...
1
vote
1answer
15 views

Initialization of dynamically allocated structures using shared pointer

I dont understand why initialization of dynamically allocated structure needs to be done like this (using shared ptr) Just to notify that I am using C++11 If we have struct like this struct Meme { ...
0
votes
1answer
42 views

'<function-style-cast>': cannot convert from 'initializer list' to 'std::shared_ptr<SDL_Window>' on creating shared_ptr with custom deleter

I created the wrapper functors for SDL2 library methods to return smart pointers with custom deleters. It seems to work fine for unqiue_ptr (class Image) but gives following error for classes ...
4
votes
1answer
197 views

How much memory do 1000 shared pointers take?

How much memory do (e.g.) 1000 shared pointers take? is it 16 x 1000 bytes? Does it differ for 32 and 64bit system?
-3
votes
2answers
53 views

Deleting last element in a std::vector of std::shared_ptr while iterating over it causes segmentation fault [duplicate]

please try to compile and run the following code. When iterating over a vector of shared pointers, I have to delete last element, this will result in a segmentation fault, but I can't understand why ...
2
votes
2answers
44 views

How to add values to list<shared_ptr<Abstract>>

I need to add elements of derived classes into list of shared pointers to abstract class. I've been trying this. I know that I am trying to create instance of abstract class in the example below, but ...
1
vote
3answers
73 views

Is it possible to remove elements from a vector of shared_ptr?

Say I have vector<shared_ptr<string>> enemy; how do I remove elements from the enemy vector? Thanks for your help in advance **Edit (code in context) void RemoveEnemy( vector<...
0
votes
2answers
27 views

What is the syntax to access a member function of a derived class named Truck in a vector of unique_ptrs that point to various Person instances?

For a concrete example, say I am given vector<shared_ptr<DClass>> cVector and DClass (which is derived from MyClass) has the following interface class DClass: public MyClass { public:...
0
votes
2answers
70 views

Copy or constref a shared_ptr?

Consider some source of data, that holds a shared_ptr (e.g. a struct member). If you have a guarantee that it is not a temporary value, but will be valid through the current scope and you want have an ...
0
votes
1answer
38 views

adding static_pointer_cast<Derived> to a std::list<shared_ptr<Base>> causes pure virtual method called error

I have this Base class class Base { std::string nome; public: std::string getName() const; virtual int mType() const = 0; virtual void ls(int indent=0) const = 0; }; from which I ...
1
vote
0answers
40 views

Which shared_ptr/object is deleted first when provided with a second? [duplicate]

Example: { std::shared_ptr<A> a(new A("First")); std::shared_ptr<A> b(a); } Which of these is the first to be deleted?
0
votes
1answer
47 views

Using a mutex in conjunction with shared_ptr to protect data shared across copies

I have a class whose copies share the same data via shared_ptr. Since that data is going to be modified by different threads I'm going to use a mutex in order to protect it, however, it is my ...
0
votes
0answers
13 views

Getting shared pointer of reference argument [duplicate]

I am currently struggling with references and smart pointers with c++, this is my class layout (Everything is child of abstract A) A +-B +-C +-D What i want is when I call B().foo(C()) is to add ...
2
votes
2answers
113 views

std::shared_ptr in one writer many reader design is thread safe?

In a mutithreading server, one thread(writer) update data periodically from a database and other thread(readers) process user's request with this data. I try to use a read/write lock to meet this ...
1
vote
1answer
96 views

How to call std::function after move to shared_ptr

I am stuck on how to call a std::function that has been moved into a shared_ptr… #include <iostream> #include <functional> using func_type = std::function<int()>; int main(int, ...
0
votes
1answer
50 views

Compiler error while using shared_ptr with a pointer to a pointer

I am new to using smart pointers in C++ and my current issue is that I am converting C code to C++ (C++11/14/17) and I am having some problems understanding using shared_ptr with a pointer to ...
0
votes
1answer
68 views

object containing smart pointer to itself that isn't reset before object goes out of scope

Ran into this bug where a class contains a pointer to itself -- and it was pointing to the same instance. Here is the smallest code snippet that contains that problem... class Node { public: ...
2
votes
2answers
49 views

C++ function header matching: how does matching work when const and templates are both involved?

I had a templated function that I wished to call. This is (a trimmed-down version of) the header: template <typename Item> void print (shared_ptr<const MyContainer<Item>> stuff, ...
0
votes
2answers
82 views

How to implement a weak reference to an object in a constructor using boost::weak_ptr?

How does one create a boost::weak_ptr to an exiting object in C++ inside a constructor? Here's what I've got but it doesn't work because the shared pointer constructor is wrong. The thing is i don't ...
2
votes
2answers
83 views

Share object between two lambdas

I am trying to share the same object (in my case, a string) between to lambdas using shared_ptr: auto fileToLoad = make_shared<string>(); StartAsync( [=]() { QDir dir(...
21
votes
3answers
1k views

Is there a way to make member function NOT callable from constructor?

I have member function (method) which uses std::enable_shared_from_this::weak_from_this() In short: weak_from_this returns weak_ptr to this. One caveat is it can't be used from constructor. If ...
0
votes
1answer
31 views

how to use std::make_shared with a singleton instance

I am trying to create a shared pointer of a singleton instance. Below is the way I am trying to do it. Singleton implementation: class MessageQueue { private: std::mutex ...
2
votes
2answers
41 views

typedef shared_ptr<T> conversion to bool

I have a typedefined shared_ptr<> which I am using in a conditional check. This is how it is defined: typedef std::shared_ptr<T> typeTPtr; Then I am passing it to a function taking a bool ...
0
votes
0answers
39 views

why syslog sink sends same records on all the sinks when a sink is member of class?

I have written following code to send logs to remote syslog(rsyslog on remote unix machine) from my windows machine using boost-log. I am using syslog_backend . #include <boost/config.hpp> #if ...
0
votes
1answer
54 views

Strange error from move and copy constructors that have shared_ptrs

I'm getting the following errors when trying compile my HuffmanNode class for an assignment, it seems to be something to do with the shared_ptrs in the class, but I have no idea as to what is actually ...