shared-ptr

I can't use std::vector of auto_ptr - should i use shared_ptr?

I don't really need to share the objects, but i do want to make sure no memory leakage occurs. Is it correct to use shared_ptr in this case? ...

How can i hold reference in boost::shared_ptr using boost::bind without definition of explicit function?

I want to hold reference to object so it doesn't get deleted in bind function, but without using helper function. struct Int { int *_int; ~Int(){ delete _int; } }; void holdReference(boost::shared_ptr<Int>, int*) {} // helper boost::shared_ptr<int> fun() { boost::shared_ptr<Int> a ( new Int ); // I get 'a' from some pleas...

The cost of passing by shared_ptr

I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function arguments. Consider the following: class Dataset {...} void f( shared_ptr< Dataset const > pds ) {...} void g( shared_ptr< Dataset const > pds ) {...} ... While passing a dataset object around via shared_ptr guarantees its e...

How does one create a shared_ptr to pass to a function that takes a void *

That's pretty much it. I need to allocate memory and pass it to a function that takes a void *. I'd like to use a shared_ptr but I don't know how to do it. ...

How to handle 'this' pointer in constructor?

I have objects which create other child objects within their constructors, passing 'this' so the child can save a pointer back to its parent. I use boost::shared_ptr extensively in my programming as a safer alternative to std::auto_ptr or raw pointers. So the child would have code such as shared_ptr<Parent>, and boost provides the shar...

Why isn't the reference counter in boost::shared_ptr volatile?

In the boost::shared_ptr destructor, this is done: if(--*pn == 0) { boost::checked_delete(px); delete pn; } where pn is a pointer to the reference counter, which is typedefed as shared_ptr::count_type -> detail::atomic_count -> long I would have expected the long to be volatile long, given threaded usage and the non-atomic ...

Is there a way to increase the efficiency of shared_ptr by storing the reference count inside the controlled object?

Hello everyone :) This is becoming a common pattern in my code, for when I need to manage an object that needs to be noncopyable because either A. it is "heavy" or B. it is an operating system resource, such as a critical section: class Resource; class Implementation : public boost::noncopyable { friend class Resource; HANDLE ...

`enable_shared_from_this` has a non-virtual destructor

I have a pet project with which I experiment with new features of the upcoming C++0x standard. While I have experience with C, I'm fairly new to C++. To train myself into best practices, (besides reading a lot), I have enabled some strict compiler parameters (using GCC 4.4.1): -std=c++0x -Werror -Wall -Winline -Weffc++ -pedantic-errors ...

Why aren't these shared_ptrs pointing to the same container?

I have a class Model: class Model { ... boost::shared_ptr<Deck> _deck; boost::shared_ptr<CardStack> _stack[22]; }; Deck inherits from CardStack. I tried to make _stack[0] point to the same thing that _deck points to by going: { _deck = boost::shared_ptr<Deck>(new Deck()); _stack[0] = _deck; } It seems that t...

Intellisense fails for boost::shared_ptr with Boost 1.40.0 in Visual Studio 2008

I'm having trouble getting intellisense to auto-complete shared pointers for boost 1.40.0. (It works fine for Boost 1.33.1.) Here's a simple sample project file where auto-complete does not work: #include <boost/shared_ptr.hpp> struct foo { bool func() { return true; }; }; void bar() { boost::shared_ptr<foo> pfoo; pfoo.get()...

tr1::shared_ptr, Eclipse, and autocomplete woes

Does anyone know if there is a plugin or some sort of fix to be able to autocomplete on variables of type tr1::shared_ptr? ...

Can I use boost::make_shared with a private constructor?

Consider the following: class DirectoryIterator; namespace detail { class FileDataProxy; class DirectoryIteratorImpl { friend class DirectoryIterator; friend class FileDataProxy; WIN32_FIND_DATAW currentData; HANDLE hFind; std::wstring root; DirectoryIteratorImpl(); ...

How to accomplish covariant return types when returning a shared_ptr?

using namespace boost; class A {}; class B : public A {}; class X { virtual shared_ptr<A> foo(); }; class Y : public X { virtual shared_ptr<B> foo(); }; The return types aren't covariant (nor are they, therefore, legal), but they would be if I was using raw pointers instead. What's the commonly accepted idiom to work around thi...

How to prevent anyone from stealing my shared_ptr?

So, I use boost::shared_ptr for all the various reference-counting benefits it provides -- reference counting for starters, obviously, but also the ability to copy, assign, and therefore store in STL Containers. The problem is, if I pass it to just one "malicious" function or object, the object can save the ptr and then I'll never be ab...

Returning references while using shared_ptrs

Suppose I have a rather large class Matrix, and I've overloaded operator== to check for equality like so: bool operator==(Matrix &a, Matrix &b); Of course I'm passing the Matrix objects by reference because they are so large. Now i have a method Matrix::inverse() that returns a new Matrix object. Now I want to use the inverse directl...

What's your convention for typedef'ing shared_ptr?

I'm flip-flopping between naming conventions for typedef'ing the boost::shared_ptr template. For example: typedef boost::shared_ptr<Foo> FooPtr; Before settling on a convention, I'd like to see what others use. What is your convention? EDIT: To those nesting the typedef inside Foo, doesn't it bother you that Foo is now "aware" of ho...

Memory leak using shared_ptr

Both code examples compile and run without problems. Using the second variant results in a memory leak. Any ideas why? Thanks in advance for any help. Variant 1: typedef boost::shared_ptr<ParameterTabelle> SpParameterTabelle; struct ParTabSpalteData { ParTabSpalteData(const SpParameterTabelle& tabelle, const string& id) ...

Is there any boost-independent version of boost/tr1 shared_ptr

I'm looking for independent implementation of boost/tr1 shared_ptr, weak_ptr and enable_shared_from_this. I need: Boost independent very small implementation of these features. I need support of only modern compilers like GCC-4.x, MSVC-2008, Intel not things like MSVC6 or gcc-3.3 I need it to be licensed under non-copyleft LGPL compa...

How to attach boost::shared_ptr (or another smart pointer) to reference counter of object's parent?

I remember encountering this concept before, but can't find it in Google now. If I have an object of type A, which directly embeds an object of type B: class A { B b; }; How can I have a smart pointer to B, e. g. boost::shared_ptr<B>, but use reference count of A? Assume an instance of A itself is heap-allocated I can safely get ...

Casting a container of shared_ptr

Hi all, I have a method void foo(list<shared_ptr<Base>>& myList); Which I'm trying to call with a two different types of lists, one of DerivedClass1 and one of DerivedClass2 list<shared_ptr<DerivedClass1>> myList1; foo(myList1); list<shared_ptr<DerivedClass2>> myList2; foo(myList2); However this obviously generates a compiler ...