shared-ptr

Does std::make_shared() use custom allocators?

余生颓废 提交于 2019-12-03 10:11:45
Consider this code : #include <memory> #include <iostream> class SomeClass { public: SomeClass() { std::cout << "SomeClass()" << std::endl; } ~SomeClass() { std::cout << "~SomeClass()" << std::endl; } void* operator new(std::size_t size) { std::cout << "Custom new" << std::endl; return ::operator new(size); } void operator delete(void* ptr, std::size_t size) { std::cout << "Custom delete" << std::endl; ::operator delete(ptr); } }; int main() { std::shared_ptr<SomeClass> ptr1(new SomeClass); std::cout << std::endl << "Another one..." << std::endl << std::endl; std::shared_ptr<SomeClass> ptr2

enable_shared_from_this and inheritance

南笙酒味 提交于 2019-12-03 09:31:17
I've got a type which inherits from enable_shared_from_this<type> , and another type that inherits from this type. Now I can't use the shared_from_this method because it returns the base type and in a specific derived class method I need the derived type. Is it valid to just construct a shared_ptr from this directly? Edit: In a related question, how can I move from an rvalue of type shared_ptr<base> to a type of shared_ptr<derived> ? I used dynamic_cast to verify that it really was the correct type, but now I can't seem to accomplish the actual move. Once you obtain the shared_ptr<Base> , you

Why is std::weak_ptr::expired optimized away?

浪子不回头ぞ 提交于 2019-12-03 09:04:36
问题 In the following code, while ( !Ref.expired() ); is joyfully optimized into an infinite loop. If the line of code is changed to while ( !Ref.lock() ); . everything works as expected. So two questions really: 1) How can the compiler optimize away expired when std::weak_ptr::expired() accesses a memory-fenced counter? 2) Is Ref.lock() actually safe, or could this too be optimized away? Sample code below. #include <iostream> #include <memory> #include <thread> #include <chrono> class A { public:

Shared pointers to a singleton do not recognize each other

爱⌒轻易说出口 提交于 2019-12-03 08:07:56
I am currently learning how to use the C++11 smart pointers while programming a 2D game engine as a hobby using SDL. However, I ran into a problem while implementing an OOp wrapper for SDL. The intent is to create a singleton class, which initializes SDL when it is constructed, and shuts SDL down when it gets destroyed. The singleton class has a static method getInstance that returns a shared_ptr to the singleton, and constructs the single instance if no instance exists, the idea being that all clients of the singleton own a shared_ptr to it, and when all clients get destroyed, the singleton

C++ weak_ptr creation performance

可紊 提交于 2019-12-03 08:07:47
I've read that creating or copying a std::shared_ptr involves some overhead (atomic increment of reference counter etc..). But what about creating a std::weak_ptr from it instead: Obj * obj = new Obj(); // fast Obj * o = obj; // slow std::shared_ptr<Obj> a(o); // slow std::shared_ptr<Obj> b(a); // slow ? std::weak_ptr<Obj> c(b); I was hoping in some faster performance, but i know that the shared pointer still have to increment the weak references counter.. So is this still as slow as copying a shared_ptr into another? Howard Hinnant In addition to Alec's very interesting description of the

How To Make a clone method using shared_ptr and inheriting from enable_shared_from_this

邮差的信 提交于 2019-12-03 07:46:16
问题 I have seen that a useful way to write a clone method that returns a boost::shared_ptr is to do class A { public: shared_ptr<A> Clone() const { return(shared_ptr<A>(CloneImpl())); } protected: virtual A* CloneImpl() const { return(new A(*this)); } }; class B : public A { public: shared_ptr<B> Clone() const { return(shared_ptr<B>(CloneImpl())); } protected: virtual B* CloneImpl() const { return(new B(*this)); } }; This allows the use of covariance with the regular pointer while still wrapping

Fully thread-safe shared_ptr implementation

断了今生、忘了曾经 提交于 2019-12-03 07:22:42
问题 Does anybody know of a fully thread-safe shared_ptr implementation? E.g. boost implementation of shared_ptr is thread-safe for the targets (refcounting) and also safe for simultaneous shared_ptr instance reads, but not writes or for read/write. (see Boost docs, examples 3, 4 and 5). Is there a shared_ptr implementation that is fully thread-safe for shared_ptr instances? Strange that boost docs say that: shared_ptr objects offer the same level of thread safety as built-in types. But if you

was raw-pointer constructor of shared_ptr a mistake?

自古美人都是妖i 提交于 2019-12-03 07:02:22
In hindsight, given make_shared , would shared_ptr have a constructor that takes a raw pointer had it been introduced with C++11? Are there strong arguments or use cases in favor of this constructor? It would have avoided the well documented pitfall of exception-safety and the memory allocation/performance advantage of using make_shared . I believe another advantage of requiring shared_ptr construction via make_shared would be that it could be a single pointer under the hood, lowering its memory use and making things like atomic_compare_exchange a lot simpler (and possibly more efficient).

c++11 std::shared_ptr + boost::serialization [duplicate]

半腔热情 提交于 2019-12-03 06:58:48
This question already has an answer here: How can boost::serialization be used with std::shared_ptr from C++11? 7 answers boost serialize and std::shared_ptr 2 answers Hi has anybody already managed to serialize a C++11 std::shared_ptr with boost::serialization. There are lots of obsolete posts out there but none with an acceptable solution. I am not going into discussion why I want to use the std::shared_ptr just accept it! I found this other post: boost serialize and std::shared_ptr but it does not answer my problem how to serialize the std::shared_ptr it only suggests to uses the boost:

Explicitly deleting a shared_ptr

主宰稳场 提交于 2019-12-03 06:47:48
问题 Simple question here: are you allowed to explicitly delete a boost::shared_ptr yourself? Should you ever? Clarifying, I don't mean delete the pointer held by the shared_ptr . I meant the actual shared_ptr itself. I know most people suggest to not do it, so I was just wondering if it's OK to explicitly do it. 回答1: Your question isn't clear. If you've allocated a shared_ptr dynamically then you're certainly allowed to delete it whenever you want. But if you're asking whether you're allowed to