How can you efficiently check whether two std::weak_ptr pointers are pointing to the same object?

Completely rewriting this answer because I totally misunderstood. This is a tricky thing to get right! The usual implementation of std::weak_ptr and std::shared_ptr that is consistent with the standard is to have two heap objects: the managed object, and a control block. Each shared pointer that refers to the same object contains a pointer to … Read more

To what degree does std::shared_ptr ensure thread-safety?

As others have pointed out, you’ve got it figured out correctly regarding your original 3 questions. But the ending part of your edit Calling reset() in thread IV will delete previous instance of A class created in first thread and replace it with new instance? Moreover after calling reset() in IV thread other threads will … Read more

Can you make a std::shared_ptr manage an array allocated with new T[]?

With C++17, shared_ptr can be used to manage a dynamically allocated array. The shared_ptr template argument in this case must be T[N] or T[]. So you may write shared_ptr<int[]> sp(new int[10]); From n4659, [util.smartptr.shared.const] template<class Y> explicit shared_ptr(Y* p); Requires: Y shall be a complete type. The expression delete[] p, when T is an array … Read more

How to properly duplicate an object given its shared_ptr

std::make_shared is just a simple template function that creates the objects, passing all arguments to the constructor : template<class T, class… Args> shared_ptr<T> make_shared(Args&&… args) { return shared_ptr<T>( new T( std::forward<Args>( args )… ) ); } In your particular case : std::shared_ptr<Event> o = std::make_shared<Event>(*e); the object is copied. If your code is such : … Read more

shared_ptr Assertion px != 0 failed

There should be no problem with using boost::shared_ptr as long as you initialize your shared pointers correctly and use the same memory management context for all your shared object libraries. In your case you are trying to use an uninitialized shared pointer. boost::shared_ptr<Obj> obj; obj->Something(); // assertion failed boost::shared_ptr<Obj> obj(new Obj); obj->Something(); // ok I … Read more

Explicitly deleting a shared_ptr

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 delete whatever object is being managed by the shared_ptr, then the answer is … it depends. If shared_ptr::unique returns true, then calling shared_ptr::reset will delete the managed … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)