shared-ptr

Shared pointer constness in comparison operator ==

瘦欲@ 提交于 2019-12-11 14:09:24
问题 I stumbled upon an unexpected behavior of a shared pointer I'm using. The shared pointer implements reference counting and detaches (e.g. makes a copy of), if neccessary, the contained instance on non-const usage. To achieve this, for each getter function the smart pointer has a const and a non-const version, for example: operator T *() and operator T const *() const . Problem: Comparing the pointer value to nullptr leads to a detach. Expected: I thought that the comparison operator would

Atomically accessing resources stored in a map

落花浮王杯 提交于 2019-12-11 11:42:32
问题 I want to store some std::shared_ptr to C++ class instances in a map, e.g. a std::map, using an integer key. However, I need this map to have two properties: If the key doesn't exist, then return an error rather than create a new object. If the key does exist then grab a copy of the std::shared_ptr atomically. I.e. it should not be possible to delete an object from the map in one thread while simultaneously retrieving it from the map in another. I'd like to avoid having a single mutex (even

Linking pthread disables lock-free shared_ptr implementation

我的未来我决定 提交于 2019-12-11 09:53:55
问题 The title pretty much conveys all relevant information, but here's a minimal repro: #include <atomic> #include <cstdio> #include <memory> int main() { auto ptr = std::make_shared<int>(0); bool is_lockless = std::atomic_is_lock_free(&ptr); printf("shared_ptr is lockless: %d\n", is_lockless); } Compiling this with the following compiler options produces a lock-free shared_ptr implementation: g++ -std=c++11 -march=native main.cpp While this doesn't: g++ -std=c++11 -march=native -pthread main.cpp

shared_ptr template argument invalid

只愿长相守 提交于 2019-12-11 06:39:16
问题 I am trying to have a static method return a shared_ptr. It is not compiling and is giving template argument 1 is invalid. I can not figure out why this is. Also, stack overflow says my post is mostly code and that I should add more detail. I don't know why this is, as being concise never hurt anyone. My problem is clear cut and and can be detailed easily. Compiler error src/WavFile.cpp:7:24: error: template argument 1 is invalid std::shared_ptr<WavFile> WavFile::LoadWavFromFile(std::string

assigning value from a vector (of shared pointer) to a shared pointer cause segmentation fault c++

≡放荡痞女 提交于 2019-12-11 06:19:05
问题 In my code, I have a vector <vector <vector <vector <std::tr1::shared_ptr<foo> > > > > named foosBoxes . The nested vector has a role of simulating a physical boxes position. I also have a while loop which cause a segmentation fault: vector<std::tr1::shared_ptr<foo> >::iterator fooit = foosBoxes[x][y][z].begin(); //x,y,z are valid integer std::tr1::shared_ptr<foo> aFoo; while (fooit != foosBoxes[x][y][z].end()){ aFoo = *fooit; //this cause segmentation fault fooit++; //some stuff which does

Trouble constructing shared_ptr

此生再无相见时 提交于 2019-12-11 04:58:48
问题 I'm new to smart pointers, and I'm in the process of hitting every stumbling block. I have a struct texture_t : struct texture_t { hash32_t hash; uint32_t width; uint32_t height; uint32_t handle; }; When I try and make a shared_ptr of this struct using this line: auto texture_shared_ptr = std::make_shared<texture_t>(new texture_t()); I get this error: error C2664: 'mandala::texture_t::texture_t(const mandala::texture_t &)' : cannot convert parameter 1 from 'mandala::texture_t *' to 'const

C++: Creating a templated Shared<T> object rather than a shared_ptr<T> object

允我心安 提交于 2019-12-11 03:20:37
问题 Per my previous question, I wish that a boost::shared_ptr<A> was actually a subclass of A (or perhaps A* ) so that it could be used in methods that took A* as their argument. Consider the following class: class A { public: A(int x) {mX = x;} virtual void setX(int x) {mX = x;} virtual int getX() const {return mX;} private: int mX; }; In the previous question, I proposed the creation of a SharedA object to take care of this, and presumably it does. class SharedA : public A { public: SharedA(A*

make_shared doesn't play along with enable_shared_from_this?

白昼怎懂夜的黑 提交于 2019-12-11 02:34:11
问题 Consider the following two code snippets, The first one: #include "pch.h" #include <memory> #include <boost/asio.hpp> using boost::asio::ip::tcp; class tcp_connection : public std::enable_shared_from_this<tcp_connection> { public: typedef std::shared_ptr<tcp_connection> pointer; static pointer create(boost::asio::io_service& io_service) { return pointer(new tcp_connection(io_service)); //second example only differs by replacing the above line with the below one //return std::make_shared<tcp

C++ shared_ptr - attach to a new raw pointer?

岁酱吖の 提交于 2019-12-11 02:25:37
问题 I think I'm missing something simple here. I'm using Boost's shared_ptr . shared_ptr<Foo> pA(new Foo()); shared_ptr<Foo> pB(new Foo()); Now, I want to switch pB so it contains the contents of pA , decrementing the ref count of pB . How can I do this? 回答1: It's all done automatically: pB = pA; // pB ref count is decrement (in this case causing the value to be released) // pB is then made to point at the same value as pA // Thus incrementing the refCount. 来源: https://stackoverflow.com/questions

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

岁酱吖の 提交于 2019-12-11 02:25:18
问题 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 I have no idea how to make it work. Simplified classes look like this: using namespace std; class Abstract { public: virtual string toString() const = 0; }; class A : public Abstract { public: A(int a, int b) : a(a), b(b) {} string toString() const { return "A"; } private: int a; int b; }; class