shared-ptr

std::shared_ptr and initializer lists

十年热恋 提交于 2019-11-29 01:25:44
The std::shared_ptr constructor isn't behaving as I expected: #include <iostream> #include <vector> void func(std::vector<std::string> strings) { for (auto const& string : strings) { std::cout << string << '\n'; } } struct Func { Func(std::vector<std::string> strings) { for (auto& string : strings) { std::cout << string << '\n'; } } }; int main(int argc, const char * argv[]) { func({"foo", "bar", "baz"}); Func({"foo", "bar", "baz"}); //auto ptr = std::make_shared<Func>({"foo", "bar", "baz"}); // won't compile. //auto ptr = std::make_shared<Func>{"foo", "bar", "baz"}; // nor this. return 0; }

Is it safe to use STL (TR1) shared_ptr's between modules (exes and dlls)

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 01:03:50
问题 I know that new-ing something in one module and delete-ing it in another can often cause problems in VC++. Problems with different runtimes. Mixing modules with staticly linked runtimes and/or dynamically linked versioning mismatches both can screw stuff up if I recall correctly. However, is it safe to use VC++ 2008's std::tr1::shared_ptr across modules? Since there is only one version of the runtime that even knows what what a shared_ptr is, static linking is my only danger (for now...). I

Why is GoogleMock leaking my shared_ptr?

倖福魔咒の 提交于 2019-11-29 01:03:35
I use GoogleMock/GoogleTest for testing, and I'm seeing some strange behavior when a matcher has a shared_ptr to a mock as a parameter, and EXPECT is called on the same shared_ptr. The offending piece of code: #include <gmock/gmock.h> #include <gtest/gtest.h> #include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> using namespace boost; using namespace testing; struct MyParameter { virtual ~MyParameter() {} virtual void myMethod() = 0; }; struct MyParameterMock : public MyParameter { MOCK_METHOD0(myMethod, void()); }; struct MyClass { virtual ~MyClass() {} virtual void myMethod(shared

shared_ptr and cyclic references

谁说我不能喝 提交于 2019-11-28 23:46:32
I was trying with the cyclic references for boost::shared_ptr , and devised following sample: class A{ // Trivial class public: i32 i; A(){} A(i32 a):i(a){} ~A(){ cout<<"~A : "<<i<<endl; } }; shared_ptr<A> changeI(shared_ptr<A> s){ s->i++; cout<<s.use_count()<<'\n'; return s; } int main() { shared_ptr<A> p1 = make_shared<A>(3); shared_ptr<A> p2 = p1; shared_ptr<A> p3 = p2; shared_ptr<A> p4 = p3; p1 = p4; // 1) 1st cyclic ref. cout<<p1.use_count()<<'\n'; p1 = changeI(p4); // 2) 2nd cyclic ref. cout<<p1.use_count()<<'\n'; // putchar('\n'); cout<<endl; } which outputs 4 5 4 ~A : 4 Is it that I've

smart pointers + “this” considered harmful?

喜你入骨 提交于 2019-11-28 20:49:43
In a C++ project that uses smart pointers, such as boost::shared_ptr , what is a good design philosophy regarding use of " this "? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-static class members intrinsically use a this pointer. It's a raw pointer and that can't be changed. If I ever store this in another variable or pass it to another function which could potentially store it for later or bind it in a callback, I'm creating bugs that

How to intentionally delete a boost::shared_ptr?

我们两清 提交于 2019-11-28 20:04:13
I have many boost::shared_ptr<MyClass> objects, and at some point I intentionally want to delete some of them to free some memory. (I know at that point that I will never need the pointed-to MyClass objects anymore.) How can I do that? I guess you can't just call delete() with the raw pointer that I get with get() . I've seen a function get_deleter(shared_ptr<T> const & p) in boost::shared_ptr , but I'm not sure how to use it, and also it says experimental right next to it. (I think I have Boost 1.38.) Maybe just assign a new empty boost::shared_ptr to the variable? That should throw away the

Deletion of pointer to incomplete type and smart pointers

前提是你 提交于 2019-11-28 18:48:12
When trying to use an auto_ptr with a type that was declared with forward-declaration, like this: class A; ... std::auto_ptr<A> a; the destructor of A is not called (apparently, because auto_ptr internally delete s the underlying pointer and the destructor for an incomplete type cannot be called). However, the same code works fine and the destructor is called when using std::shared_ptr instead of std::auto_ptr . How can that be explained? A shared_ptr can be declared with an incomplete type, yes. The type does not need to be complete until you initialize or reset it. When you initialize or

How does a reference-counting smart pointer's reference counting work?

不羁岁月 提交于 2019-11-28 18:47:05
问题 In other words, how does the implementation keeps track of the count? Is there a map-like object maintained which is accessible by all the shared_ptr instances whose key is the pointer's address and value is the number of references? If I've to implement a shared_ptr , this is the first idea that's coming to my mind. Is there a possibility for a memory leak in case of these reference-counting smart pointers? If so, how can I avoid them? 回答1: I've seen two different non-intrusive approaches to

How to initialize a shared_ptr that is a member of a class?

。_饼干妹妹 提交于 2019-11-28 18:36:45
I am not sure about a good way to initialize a shared_ptr that is a member of a class. Can you tell me, whether the way that I choose in C::foo() is fine, or is there a better solution? class A { public: A(); }; class B { public: B(A* pa); }; class C { boost::shared_ptr<A> mA; boost::shared_ptr<B> mB; void foo(); }; void C::foo() { A* pa = new A; mA = boost::shared_ptr<A>(pa); B* pB = new B(pa); mB = boost::shared_ptr<B>(pb); } Your code is quite correct (it works), but you can use the initialization list, like this: C::C() : mA(new A), mB(new B(mA.get()) { } Which is even more correct and as

Why is shared_ptr<void> legal, while unique_ptr<void> is ill-formed?

落花浮王杯 提交于 2019-11-28 17:07:57
The question really fits in the title: I am curious to know what is the technical reason for this difference, but also the rationale ? std::shared_ptr<void> sharedToVoid; // legal; std::unique_ptr<void> uniqueToVoid; // ill-formed; It is because std::shared_ptr implements type-erasure, while std::unique_ptr does not. Since std::shared_ptr implements type-erasure, it also supports another interesting property, viz. it does not need the type of the deleter as template type argument to the class template. Look at their declarations: template<class T,class Deleter = std::default_delete<T> > class