shared-ptr

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

被刻印的时光 ゝ 提交于 2019-11-30 18:24:12
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 deleted when both dll and exe don't reference it any more. bsegraves According to Scott Meyers in

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

天大地大妈咪最大 提交于 2019-11-30 18:14:52
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_ptr and if yes, how can I do this? Logically such a scenario doesn't make sense to me. Suppose for a

How does a weak_ptr know that the shared resources has expired?

痞子三分冷 提交于 2019-11-30 18:11:14
Considering the following code: #include <memory> #include <iostream> using namespace std; struct MySharedStruct { int i; }; void print_value_of_i(weak_ptr<MySharedStruct> weakPtr) { if (shared_ptr<MySharedStruct> sp = weakPtr.lock()) { cout << "Value of i = " << sp->i << endl; } else { cout << "Resource has expired"; } } int main() { shared_ptr<MySharedStruct> sharedPtr(new MySharedStruct() ); sharedPtr->i = 5; weak_ptr<MySharedStruct> weakPtr; weakPtr = sharedPtr; print_value_of_i(weakPtr); sharedPtr.reset(new MySharedStruct() ); // <<----- How does weak_ptr know it has expired after this

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

佐手、 提交于 2019-11-30 17:59:09
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/shared_ptr.h:93:11: note: function has been explicitly marked deleted here class shared_ptr : public _

Getting a unique_ptr for a class that inherits enable_shared_from_this

只谈情不闲聊 提交于 2019-11-30 17:25:19
问题 Usually I prefer returning unique_ptr from Factories. Recently I came to the problem of returning a unique_ptr for a class that inherits enable_shared_from_this . Users of this class may accidentally cause a call to shared_from_this() , though it is not owned by any shared_ptr , which results with a std::bad_weak_ptr exception (or undefined behavior until C++17, which is usually implemented as an exception). A simple version of the code: class Foo: public enable_shared_from_this<Foo> { string

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

倾然丶 夕夏残阳落幕 提交于 2019-11-30 17:08:09
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::dynamic_cast_tag)' being compiled 1> with 1> [ 1> T=my_namespace::B 1> ] 1> [location]\[source_filename].cpp

Does enable_shared_from_this and make_shared provide the same optimization

家住魔仙堡 提交于 2019-11-30 13:57:46
问题 As I understand make_shared<T>(...) may provide some memory allocation optimization (it may allocate reference counter within same memory block as instance of class T). Do enable_shared_from_this provides the same optimization? So: class T : std::enable_shared_from_this<T> {}; ... auto t = std::shared_ptr<T>(new T); Is the same as: class T {}; ... auto t = std::make_shared<T>(); If not take in account sizeof(T). 回答1: Do enable_shared_from_this provides the same optimization? So: No. As you

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

拜拜、爱过 提交于 2019-11-30 11:22:43
问题 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

C++ smart pointer const correctness

余生长醉 提交于 2019-11-30 11:18:16
问题 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

Does the standard behavior for deleters differ between shared_ptr and unique_ptr in the case of null pointers?

谁都会走 提交于 2019-11-30 10:49:53
OK, so first some things that might be relevant: I'm using the Clang 3.1 compiler, in C++11 mode, with the standard library set to libc++. I'm trying to familiarize myself with C++11, and in so doing I ran across behavior that seems odd. It may be a quirk of Clang or libc++ but I can't speak C++ standardese and I have no access to other compilers with C++11 support so I can't really check it, and I've searched the internet and Stack Overflow to the best of my ability without finding anything related...so here we go: When using shared_ptr / unique_ptr to implement RAII for a simple resource, it