shared-ptr

shared_ptr: what's it used for

Hi all, I make a lot of use of boost::scoped_ptr in my code and it is great but I'm currently working with software that uses shared_ptr all over the place and I'm wondering if I'm missing something. AFAIK a shared_ptr is only useful if different threads are going to be accessing the same data and you don't know what order the threads...

Are data structures an appropriate place for shared_ptr?

I'm in the process of implementing a binary tree in C++. Traditionally, I'd have a pointer to left and a pointer to right, but manual memory management typically ends in tears. Which leads me to my question... Are data structures an appropriate place to use shared_ptr? ...

smart pointers + "this" considered harmful?

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-sta...

A Question On Smart Pointers and Their Inevitable Indeterminism

I've been extensively using smart pointers (boost::shared_ptr to be exact) in my projects for the last two years. I understand and appreciate their benefits and I generally like them a lot. But the more I use them, the more I miss the deterministic behavior of C++ with regarding to memory management and RAII that I seem to like in a prog...

Fully thread-safe shared_ptr implementation

Does anybody know of a fully thread-safe shared_ptr implementation? E.g. boost implementation of shared_ptr is thread-safe for the targets (refcounting) and also safe for simultaneous shared_ptr instance reads, but not writes or for read/write. (see Boost docs, examples 3, 4 and 5). Is there a shared_ptr implementation that is fully th...

shared_ptr in std::tr1

I am working on a platform with a gcc compiler however boost cannot compile on it. I am wondering what is the proper way to include the shared_ptr in std:tr1 on gcc? the file i looked in said not to include it directly, from what i can tell no other file includes it either :| ...

getting a normal ptr from shared_ptr ?

i have something like shared_ptr t(makeSomething(), mem_fun(&Type::deleteMe)) i now need to call C styled func that require a pointer to Type. How do i get it from shared_ptr? ...

NULL pointer with boost::shared_ptr?

What's the equivalent to the following: std::vector<Foo*> vec; vec.push_back(NULL); when dealing with boost::shared_ptr? Is it the following code? std::vector< boost::shared_ptr<Foo> > vec; vec.push_back(boost::shared_ptr<Foo>()); Note: I may push back a lot of such objects. Should I declare a global static nullPtr object somewhere...

How to intentionally delete a boost::shared_ptr?

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 functio...

static_cast with boost::shared_ptr?

What is the equivalent of a static_cast with boost::shared_ptr? In other words, how do I have to rewrite the following Base* b = new Base(); Derived* d = static_cast<Derived*>(b); when using shared_ptr? boost::shared_ptr<Base> b(new Base()); boost::shared_ptr<Derived> d = ??? ...

Is boost shared_ptr <XXX> thread safe?

I have a question about boost :: shared_ptr. There are lots of thread. class CResource { xxxxxx } class CResourceBase { public: void SetResource(shared_ptr<CResource> res) { m_Res = res; } shared_ptr<CResource> GetResource() { return m_Res; } private: shared_ptr<CResource> m_Res; } CResourceBase base; ...

Difference between ATL CAutoPtr and STL std::auto_ptr?

I'm writing some code in MFC and I want to use auto pointers. I've come across two different classes that look like they do the same thing: CAutoPtr and std::auto_ptr What are people's thoughts about the two different implementations? Further, I know there is std::tr1::shared_ptr. Is there a similar shared_ptr that is in ATL/MFC? ...

What are potential dangers when using boost::shared_ptr?

What are some ways you can shoot yourself in the foot when using boost::shared_ptr? In other words, what pitfalls do I have to avoid when I use boost::shared_ptr? ...

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

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 possibil...

How to detect cycles when using shared_ptr

shared_ptr is a reference counting smart pointer in the Boost library. The problem with reference counting is that it cannot dispose of cycles. I am wondering how one would go about solving this in C++. Please no suggestions like: "don't make cycles", or "use a weak_ptr". Edit I don't like suggestions that say to just use a weak_ptr...

How should you return *this with a shared_ptr?

See also: Similar question The code below is obviously dangerous. The question is: how do you do keep track of the reference to *this? using namespace boost; // MyClass Definition class MyClass { public: shared_ptr< OtherClass > createOtherClass() { return shared_ptr< OtherClass > OtherClass( this ); // baaad } MyCl...

construct two shared_ptr objects from the same pointer

hello, I have a problem from "The C++ Standard Library Extensions": Exercise 6 I said in Section 2.4.2 that you shouldn't construct two shared_ptr objects from the same pointer. The danger is that both shared_ptr objects or their progeny will eventually try to delete the resource, and that usually leads to trouble. In...

boost::shared_ptr use_count

I'm trying to understand what's going on in the following code. When object-a is deleted, does it's shared_ptr member variable object-b remains in memory because object-c holds a shared_ptr to object-b? class B { public: B(int val) { _val = val; } int _val; }; class A { public: A() { _b = n...

C++ boost shared_ptr as a hash_map key

Hi all, I'm making a neural network and wanted to use a hash_map to keep weight references for output neurons for each neuron: class Neuron; //forward declaration was there (sorry I forgot to show it earlier) typedef double WEIGHT; typedef stdext::hash_map<boost::shared_ptr<Neuron>,WEIGHT> NeuronWeightMap; class Neuron { private: N...

Collection specialized for shared_ptr

Does there exist a collection, that is aware of shared_ptr internals, and avoids regular copying of stored shared_ptr elements in favor of just copying their internal weak pointer? This implicitly means, that no constructor/destructor calls will be done and that there will be no manipulation of shared_ptrs' reference counters. ...