smart-pointers

How to create a cyclic reference with Arc and Weak?

做~自己de王妃 提交于 2019-12-23 12:45:11
问题 I have two structs: struct A { map: HashMap<u32, Vec<B>>, } struct B { weak: Weak<A> } When A is constructed, it will own several B , each of which links back to the A which was just constructed, similar to this: let a = Arc::new(A { map: HashMap::new() }); let b1 = B { weak: Arc::downgrade(&a) }; let b3 = B { weak: Arc::downgrade(&a) }; let b2 = B { weak: Arc::downgrade(&a) }; a.map.insert(5, vec![b1, b2]); a.map.insert(10, vec![b3]); Playground This doesn't work since Arc does not provide a

Using std::unique_ptr for managing COM objects

萝らか妹 提交于 2019-12-23 12:39:02
问题 I'm trying to use smart pointers to hold COM objects in my class while avoiding ComPtr. Is it possible to use unique_ptr for this purpose? I'm quite new to smart pointers and so far I'm a bit confused. Please consider the following simplified code: class Texture { private: struct ComDeleter { operator() (IUnknown* p) { p.Release(); delete p; } } ID3D11Texture* m_dumbTexture; std::unique_ptr<ID3D11Texture, ComDeleter> m_smartTexture; public: ID3D11Texture* getDumbTexture() const { return m

Is there a smart pointer that is automatically nulled when its target is destroyed in C++

前提是你 提交于 2019-12-23 12:37:19
问题 I've found QPointer. Are there any others? 回答1: Boost - the weak_ptr has some nice features that make it safe to use, if you are also using shared_ptr . You keep a weak_ptr reference to an instance that is managed by shared_ptr lifetime. When you need to use the underlying instance, convert it to a shared_ptr instance using the constructor of the shared_ptr class, or the lock method. The operation will fail if the underlying instance was deleted. The use is thread safe in the same fashion as

unique_ptr is not getting init with default deleter

只愿长相守 提交于 2019-12-23 09:34:09
问题 When I create an unique_ptr with deleter , it works: std::unique_ptr<Animal<Cat>, void(*)(Animal<Cat>*)> ptr(new Animal<Cat>, [](Animal<Cat> *ls) { delete ls; }); But, this code is throwing error: std::unique_ptr<Animal<Cat>, void(*)(Animal<Cat>*)> ptr; ptr = std::unique_ptr<Animal<Cat>, void(*)(Animal<Cat>*)>(new Animal<Cat>, [](Animal<Cat> *ls) { delete ls; }); The error: /usr/bin/../lib/c++/v1/memory:2561:13: error: static_assert failed "unique_ptr constructed with null function pointer

Deleting vector of pointers

ぃ、小莉子 提交于 2019-12-23 07:39:38
问题 I need to create pointers of instances of a class, and the program do not know at compilation time how many pointers I will create. For deletion, I was considering storing the pointers in a vector, and then deleting them one by one. Would the use of smart pointers a cleaner way to go ? And if one does not want to use smart pointers, would this use of vector be considered clean ? Minimum code: #include <vector> using namespace std; class Foo { public: Foo(); }; Foo::Foo(){} void

RAII in C++/CLI

我怕爱的太早我们不能终老 提交于 2019-12-22 04:37:26
问题 I'm used to the C++ RAII facilities, and I want to use RAII the right way with managed code in C++/CLI. Herb Sutter and Microsoft both tell me this is the best practice. I have something like this: ref struct Managed { // No default constructor Managed( /*...*/ ) { /*...*/ } ~Managed() { /* Important non-managed resource release here */ } // ... }; ref struct UsesManaged { Managed^ m_; array<Managed^>^ a_; UsesManaged( Managed^ m, array<Managed^>^ a ) : m_(m), a_(a) {} // ... }; ref struct

How to make_shared a derived class?

爷,独闯天下 提交于 2019-12-22 01:28:30
问题 I want to use the make_shared<T> function with a derived class, like below class Base { public: typedef std::shared_ptr<Base> Ptr; }; class Derived : public Base {}; Base::Ptr myPtr = std::make_shared(/* Derived() */ ); How can I tell make_shared to build such an object? I want to avoid the classical Base::Ptr ptr = Base::Ptr(new Derived()); To make use of the single alloc in the make_shared function. 回答1: std::shared_ptr has a converting constructor that can make a shared_ptr<Base> from a

What does “single allocation” mean for boost::make_shared

回眸只為那壹抹淺笑 提交于 2019-12-21 09:09:09
问题 In the boost doc of make_shared, it says: Besides convenience and style, such a function is also exception safe and considerably faster because it can use a single allocation for both the object and its corresponding control block , eliminating a significant portion of shared_ptr's construction overhead. I don't understand the meaning of "single allocation", what does it mean? 回答1: An "allocation" means a block of memory obtained from a call to an allocator. Usually, creating a shared_ptr

What does “single allocation” mean for boost::make_shared

拜拜、爱过 提交于 2019-12-21 09:08:29
问题 In the boost doc of make_shared, it says: Besides convenience and style, such a function is also exception safe and considerably faster because it can use a single allocation for both the object and its corresponding control block , eliminating a significant portion of shared_ptr's construction overhead. I don't understand the meaning of "single allocation", what does it mean? 回答1: An "allocation" means a block of memory obtained from a call to an allocator. Usually, creating a shared_ptr

Should boost::ptr_vector be used in place std::vector all of the time?

六眼飞鱼酱① 提交于 2019-12-21 04:55:16
问题 Just a conceptual question that I've been running into. In my current project it feels like I am over-using the boost smart_ptr and ptr_container libraries. I was creating boost::ptr_vectors in many different objects and calling the transfer() method to move certain pointers from one boost::ptr_vector to another. It is my understanding that it is important to clearly show ownership of heap allocated objects. My question is, would it be desirable to use these boost libraries to create heap