shared-ptr

Segmentation fault when using a shared_ptr

烈酒焚心 提交于 2019-12-23 03:32:14
问题 I am making a particle system and I'm struggling with how to structure my code. The idea is that a user can create one or several ParticleEmitter objects that are passed to a ParticleManager object via the ofxCurlNoise object. Now, I want that when the user updates the ParticleEmitters objects, the ParticleManager object sees the changes made. So I used shared pointers but I have segmentation faults at different times, whether I use one ParticleEmitter (segmentation fault when the program

Why calling shared_from_this calls std::terminate

纵饮孤独 提交于 2019-12-22 12:19:30
问题 Consider this code: class A : public std::enable_shared_from_this<A> { public: std::shared_ptr<A> f() { return shared_from_this(); } }; int main() { A a; std::shared_ptr<A> ptr = a.f(); } This code terminated in Visual Studio 2017. I guess I am doing something wrong here. Can anyone help me with this? I want a shared_ptr on a created by shared_from_this(). 回答1: Because a is not a owned by a shared pointer. From cppreference: It is permitted to call shared_from_this only on a previously shared

C++ - pointer passing question

橙三吉。 提交于 2019-12-22 10:58:45
问题 Does somebody have any idead on how to pass boost::shared_ptr - by value or by reference. On my platform (32bit) sizeof(shared_ptr) equals 8 bytes and it looks like I should pass them by reference, but maybe somebody has another opinion / did a profile / something like that? 回答1: You can see this in two ways: a boost::shared_ptr is an object (and should be passed by const &). a boost::shared_ptr models a pointer and should be treated as a pointer. Both of them are valid, and the second option

boost.python expose function that returns vector<MyClass>

只谈情不闲聊 提交于 2019-12-22 10:56:32
问题 I'm writing an extension module for Python in C++ and I am using boost.python. I want to expose a function that returns a vector<MyClass> . I'm not exactly sure how to do this and how it will interact with Python WRT memory management. My first thought was to wrap MyClass in shared_ptr , thus the function would return vector<shared_ptr<MyClass>> . Would this help? What happens when shared_ptr<MyClass> instances get to Python land? Will they ever be freed? So my question is: how can I expose a

c++: how to return a shared_ptr from function

心已入冬 提交于 2019-12-22 10:21:17
问题 when trying to return a shared_ptr from a function I get: reference to local variable 'recipe' returned [-Werror=return-local-addr] where did I go wrong ? shared_ptr<Recipe>& Group::addRecipe(const string& groupName, unsigned int autherId, const string& recipeName){ shared_ptr<Recipe> recipe(new Recipe(recipeName, autherId)); recipes.push_back(recipe); return recipe; } what is the right way to return a shared_ptr ? 回答1: The function's signature isn't shown, but it sounds like it's probably

C++ interface between raw pointers and shared_ptr

ⅰ亾dé卋堺 提交于 2019-12-22 09:10:14
问题 I have code that uses raw pointers throughout. It needs to call a method that takes the raw pointer into a shared_ptr. This method is not under my control, belonging to an external api. I cannot just pass the pointer to the shared_ptr because when it will be deleted when the shared_ptr goes out of scope in the method (when the method returns). Do I have any option other than making my raw pointer a shared_ptr in my internal code? 回答1: This sounds somewhat unusual and potentially quite

What happens if I reset a std::shared_ptr to itself

强颜欢笑 提交于 2019-12-22 06:57:00
问题 The following program crashes with a bad glibc double free error: #include <iostream> #include <memory> class foo { public: foo() { std::cout << "foo constructed" << std::endl; } ~foo() { std::cout << "foo destructed" << std::endl; } }; int main() { auto f = std::make_shared< foo >(); std::cout << "Before reset" << std::endl; f.reset( f.get() ); std::cout << "After reset" << std::endl; return 0; } From this I get the following output (followed by the glibc error): foo constructed Before reset

Why is raw pointer to shared_ptr construction allowed in all cases?

☆樱花仙子☆ 提交于 2019-12-22 06:31:31
问题 I was reading Top 10 dumb mistakes to avoid with C++11 smart pointer. Number #5 reads: Mistake # 5 : Not assigning an object(raw pointer) to a shared_ptr as soon as it is created ! int main() { Aircraft* myAircraft = new Aircraft("F-16"); shared_ptr<aircraft> pAircraft(myAircraft); ... shared_ptr<aircraft> p2(myAircraft); // will do a double delete and possibly crash } and the recommendation is something like: Use make_shared or new and immediately construct the pointer with it. Ok, no doubt

using std::shared_ptr with a protected constructor\destructor [duplicate]

半世苍凉 提交于 2019-12-22 06:11:59
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I call ::std::make_shared on a class with only protected or private constructors? I want to create a shared pointer to a class, and have a factory method that returns it while keeping the constructor\destructor protected. since the shared pointer can't access the the constructor or the destructor, I get compiler errors. I am using llvm 4.1, but I am looking for a solution that can be compiler independent

using std::shared_ptr with a protected constructor\destructor [duplicate]

白昼怎懂夜的黑 提交于 2019-12-22 06:10:26
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I call ::std::make_shared on a class with only protected or private constructors? I want to create a shared pointer to a class, and have a factory method that returns it while keeping the constructor\destructor protected. since the shared pointer can't access the the constructor or the destructor, I get compiler errors. I am using llvm 4.1, but I am looking for a solution that can be compiler independent