shared-ptr

Is it OK to use boost::shared ptr in DLL interface?

丶灬走出姿态 提交于 2019-11-30 03:00:32
问题 Is it valid to develop a DLL in C++ that returns boost shared pointers and uses them as parameters? So, is it ok to export functions like this? 1.) boost::shared_ptr<Connection> startConnection(); 2.) void sendToConnection(boost::shared_ptr<Connection> conn, byte* data, int len); In special: Does the reference count work across DLL boundaries or would the requirement be that exe and dll use the same runtime? The intention is to overcome the problems with object ownership. So the object gets

Using std::shared_ptr with clang++ and libstdc++

痴心易碎 提交于 2019-11-30 01:51:39
问题 I'm trying to use the std::shared_ptr in clang++(clang version 3.1 (trunk 143100)) using libstdc++(4.6.1). I have a little demo program: #include <memory> int main() { std::shared_ptr<int> some(new int); std::shared_ptr<int> other(some); return 0; } which can be build using: clang++ -std=c++0x -o main main.cpp and gives the following error output: main.cpp:6:23: error: call to deleted constructor of 'std::shared_ptr<int>' std::shared_ptr<int> other(some); ^ ~~~~ /usr/include/c++/4.6/bits

Move ownership from std::shared_ptr to std::unique_ptr

限于喜欢 提交于 2019-11-30 01:17:50
问题 I have a class A which has a field of type std::unique_ptr : class A { public: std::unique_ptr pointer; // class body }; And somewhere in code, I'm using few std::shared_ptr s which point to the same object. Now what I'd like to achieve is to move ownership to this std::unique_ptr in my class, so that if all shared_ptr s will be destroyed, my object will stay alive as long as this unique_ptr will stay alive. My question is - is it possible to move ownership from std::shared_ptr to std::unique

C++ smart pointer const correctness

邮差的信 提交于 2019-11-30 01:14:38
I have a few containers in a class, for example, vector or map which contain shared_ptr's to objects living on the heap. For example template <typename T> class MyExample { public: private: vector<shared_ptr<T> > vec_; map<shared_ptr<T>, int> map_; }; I want to have a public interface of this class that sometimes returns shared_ptrs to const objects (via shared_ptr<const T> ) and sometimes shared_ptr<T> where I allow the caller to mutate the objects. I want logical const correctness, so if I mark a method as const, it cannot change the objects on the heap. Questions: 1) I am confused by the

C++: “… is not a polymorphic type” while using boost::dynamic_pointer_cast

删除回忆录丶 提交于 2019-11-30 00:30:35
问题 Why do I receive the following error for the following code? 1>C:\Libs\boost_1_44\boost/smart_ptr/shared_ptr.hpp(259): error C2683: 'dynamic_cast' : 'my_namespace::A' is not a polymorphic type 1> D:\[location]\[header_filename].h(35) : see declaration of 'my_namespace::A' 1> C:\Libs\boost_1_44\boost/smart_ptr/shared_ptr.hpp(522) : see reference to function template instantiation 'boost::shared_ptr<T>::shared_ptr<my_namespace::A>(const boost::shared_ptr<my_namespace::A> &,boost::detail:

How to pass deleter to make_shared?

穿精又带淫゛_ 提交于 2019-11-30 00:26:28
Since C++11, because of several reasons, developers tend to use smart pointer classes for dynamic lifetime objects. And with those new smart pointer classes, standards, even suggest to not use operators like new instead they suggest to use make_shared or make_unique to avoid some error prone. If we like to use a smart pointer class, like shared_ptr , we can construct one like, shared_ptr<int> p(new int(12)); Also we would like to pass a custom deleter to smart pointer classes, shared_ptr<int> p(new int(12), deleter); On the other hand, if we like to use make_shared to allocate, for ex. int ,

C++11 atomics and intrusive shared pointer reference count

放肆的年华 提交于 2019-11-30 00:18:19
I am writing intrusive shared pointer and I am using C++11 <atomic> facilities for reference counter. Here are the relevant fragments of my code: //... mutable std::atomic<unsigned> count; //... void SharedObject::addReference() const { std::atomic_fetch_add_explicit (&count, 1u, std::memory_order_consume); } void SharedObject::removeReference() const { bool destroy; destroy = std::atomic_fetch_sub_explicit (&count, 1u, std::memory_order_consume) == 1; if (destroy) delete this; } I have started with memory_order_acquire and memory_order_release first but then I convinced myself that memory

Can you use a shared_ptr for RAII of C-style arrays?

微笑、不失礼 提交于 2019-11-30 00:02:42
I'm working on a section of code that has many possible failure points which cause it to exit the function early. The libraries I'm interacting with require that C-style arrays be passed to the functions. So, instead of calling delete on the arrays at every exit point, I'm doing this: void SomeFunction(int arrayLength) { shared_ptr<char> raiiArray(new char[arrayLength]); pArray = raiiArray.get(); if(SomeFunctionThatRequiresCArray(pArray) == FAILED) { return; } //etc. } I wanted to use unique_ptr , but my current compiler doesn't support it and the reference count overhead doesn't really matter

How does one downcast a std::shared_ptr?

大城市里の小女人 提交于 2019-11-29 20:23:38
Consider: struct SomethingThatsABase { virtual bool IsChildOne() const { return false; } virtual bool IsChildTwo() const { return false; } }; struct ChildOne : public SomethingThatsABase { virtual bool IsChildOne() const { return true; } }; struct ChildTwo : public SomethingThatsABase { virtual bool IsChildTwo() const { return true; } }; void SomeClientExpectingAChildOne(std::shared_ptr<ChildOne> const& ptrOne) { //Does stuff } void SomeClient(std::shared_ptr<SomethingThatsABase> const& ptr) { if (ptr->IsChildOne()) { SomeClientExpectingAChildOne(ptr); //Oops. //Hmm.. can't static_cast here,

boost, shared ptr Vs weak ptr? Which to use when?

白昼怎懂夜的黑 提交于 2019-11-29 19:36:37
In my current project I am using boost::shared_ptr quite extensively. Recently my fellow team mates have also started using weak_ptr . I don't know which one to use and when. Apart from this, what should I do if I want to convert weak_ptr to shared_ptr . Does putting a lock on weak_ptr to create a shared_ptr affect my code in other thread? Narfanator In general and summary, Strong pointers guarantee their own validity. Use them, for example, when: You own the object being pointed at; you create it and destroy it You do not have defined behavior if the object doesn't exist You need to enforce