shared-ptr

Shared ownership of IDisposable objects in C#

余生颓废 提交于 2019-12-04 21:40:46
Is there any classes in C# which provide shared ownership of IDisposable objects? Something like shared_ptr in c++? And if not, what are best practices here? UPDATE I'm writing a c++/cli wrapper over native lib. And I need release native resources ( MAPI COM interfaces for example, so I need determenistic resource releasing ). Native part: //Message.h class Message { ... }; //MessageSet.h class MessageSet { ... class iterator : public std::iterator<std::forward_iterator_tag, Message*> { ... public: Message* operator*(); bool operator!=(const iterator& that); iterator& operator++(); }; iterator

std::shared_ptr deep copy object

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 18:48:53
问题 Can't find much on that for C++11 but only on boost. Consider the following class: class State { std::shared_ptr<Graph> _graph; public: State( const State & state ) { // This is assignment, and thus points to same object this->_graph = std::make_shared<Graph>( state._graph ); // Deep copy state._graph to this->_graph ? this->_graph = std::shared_ptr<Graph>( new Graph( *( state._graph.get() ) ) ); // Or use make_shared? this->_graph = std::make_shared<Graph>( Graph( *( state._graph.get() ) ) )

why is std::shared_ptr using atomic cpu operations

删除回忆录丶 提交于 2019-12-04 18:11:01
问题 I have a problem understanding why shared_ptr is using atomic cpu instructions... I cant figure out the reasons because it is NOT thread safe. Can somebody please explain. If you wonder know how I know that it uses atomic intstuructions: there was a clip from C++ and beyond where Herb and Andrei talk about it, but they never mention the reasons why is it like that. 回答1: Any instance of shared_ptr is multi-thread safe. The data it points to is not multi-thread safe. See this. The atomic

boost shared_ptr: difference between operator= and reset?

巧了我就是萌 提交于 2019-12-04 18:05:14
问题 Are there any differences between the two pieces of code below? Is any of them preferable to the other? operator= boost::shared_ptr<Blah> foo; // foo.ptr should be NULL foo = boost::shared_ptr<Blah>(new Blah()); // Involves creation and copy of a shared_ptr? reset boost::shared_ptr<Blah> foo; // foo.ptr should be NULL foo.reset(new Blah()); // foo.ptr should point now to a new Blah object Note: I need to define the shared_ptr and then set it in a different line because I'm using it in a piece

Is an empty aliasing shared_ptr a good alternative to a no-op deleting shared_ptr?

﹥>﹥吖頭↗ 提交于 2019-12-04 17:48:32
问题 Sometimes I need shared_ptr instances that have a no-op deleter, because an API expects a shared_ptr instance that it wants to store for a limited time but I am given a raw pointer that I am not allowed to own for a time larger than what I am running for. For this case, I have been using a no-op deleter, such as [](const void *){} , but today I found that there's another alternative to that, using (or abusing?) the aliasing constructor of shared_ptr : void f(ExpectedClass *ec) { std::shared

Using boost::shared_ptr in a library's public interface

只谈情不闲聊 提交于 2019-12-04 17:42:19
问题 We have a C++ library that we provide to several different clients. Recently we made the switch from using raw pointers in the public interface to using boost::sharedptr instead. This has provided an enormous benefit, as you might guess, in that now the clients no longer have to worry about who needs to delete what and when. When we made the switch I believed it was the right thing to do, but it bothered me that we had to include something from a third-party library in our public interface -

How do I implement polymorphism with std::shared_ptr?

为君一笑 提交于 2019-12-04 16:28:11
问题 I have seen some of the other questions on this topic, but have still not found the answer - I guess I'm missing something: I defined two simple test classes: class TestBase { public: TestBase ( ) { }; ~ TestBase ( ) { }; protected: inline virtual int getInt ( ) { return 0; } }; class TestDerived : public TestBase { protected: inline int getInt ( ) override { return 1; } }; I declared typedefs to simplify their usage with std::shared_ptr: typedef std::shared_ptr<TestBase> spBase; typedef std:

Does std::make_shared() use custom allocators?

大兔子大兔子 提交于 2019-12-04 16:09:42
问题 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);

Does the WKWYL optimization in make_shared<>() introduce a penalty for some multithreading applications?

爷,独闯天下 提交于 2019-12-04 16:09:12
问题 A few days ago I happened to watch this very interesting presentation by Stephan T. Lavavej, which mentions the " We Know Where You Live " optimization (sorry for using the acronym in the question title, SO warned me the question might have been closed otherwise), and this beautiful one by Herb Sutter on machine architecture. Briefly, the " We Know Where You Live " optimization consists in placing the reference counters on the same memory block as the object which make_shared is creating,

enable_shared_from_this and inheritance

陌路散爱 提交于 2019-12-04 15:15:51
问题 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