shared-ptr

How do I know who holds the shared_ptr<>?

I use boost::shared_ptr in my application in C++. The memory problem is really serious, and the application takes large amount of memory. However, because I put every newed object into a shared_ptr, when the application exits, no memory leaking can be detected. There must be some thing like std::vector<shared_ptr<> > pool holding the r...

Differences between different flavours of shared_ptr

Are there any differences between boost::shared_ptr, std::tr1::shared_ptr and the upcoming (in C++0x) std::shared_ptr? Will porting from one to another have any overhead or are they basically the same? ...

boost::shared_ptr and multithreaded access

Hi! I'm trying to implement a multithreaded framework, in which output objects are created at the end of every frame that my networking thread runs, so that another thread can, at the beginning of its frame, obtain the most recent "completed output" pointer and know that it has safe and complete read-only access to any data stored withi...

When would you use an std::auto_ptr instead of boost::shared_ptr?

We've pretty much moved over to using boost::shared_ptr in all of our code, however we still have some isolated cases where we use std::auto_ptr, including singleton classes: template < typename TYPE > class SharedSingleton { public: static TYPE& Instance() { if (_ptrInstance.get() == NULL) _ptrInstance.rese...

shared_ptr and references in C++

References in C++ are a conveneint construct that allow us to simplify the following C code: f(object *p){ //do something } int main(){ object* p = (object*) calloc(sizeof(object)); f(p); } to f(object& o){ //do something } int main(){ object o = object(); f(o); } Shared pointers are another convenience in C++ that s...

What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for?

boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p); and I am a little puzzled as to what this would be useful for. Basically it shares ownership with r, but .get() will return p. not r.get()! This means you can do something like this: int main() { boost::shared_ptr<int> x(ne...

Manually incrementing and decrementing a boost::shared_ptr?

Is there a way to manually increment and decrement the count of a shared_ptr in C++? The problem that I am trying to solve is as follows. I am writing a library in C++ but the interface has to be in pure C. Internally, I would like to use shared_ptr to simplify memory management while preserving the ability to pass a raw pointer through...

Decent shared_ptr implementation that does not require a massive library?

I am taking a C++ programming class right now on GIS Programming. I am really starting to get alot of headaches from dealing with proper memory management. Considering at any time there is often 8-10 classes each holding a pointer to a 3D matrix or something else very large. Now our class already raised the issue of the prof allowing us ...

Get Eclipse CDT + boost::shared_ptr<T> to work with syntax completion?

How to get Eclipse CDT to treat shared_ptr as T * for syntax completion? I'm using windows in this instance. I have 1.39 in the "Program Files" folder. I am about to try 1.37. I am using the Galileo release of Eclipse. Also, I am only editing and browsing the source in Eclipse and building in VC++ Express. (but that is another story) ...

C++ reference to a shared_ptr vs reference

All, I recently posted this question on DAL design. From that it would seem that passing a reference to an object into a function, with the function then populating that object, would be a good interface for a C++ Data Access Layer, e.g. bool DAL::loadCar(int id, Car& car) {} I'm now wondering if using a reference to a boost::shar...

How to fix heap corruption

I've tried to build a very minimalistic memory read library to read some unsigned ints out of it. However, I run into a "HEAP CORRUPTION DETECTED" error message when the ReadUnsignedInt method wants to return. HEAP CORRUPTION DETECTED. CRT detected that the application wrote to memory after end of buffer. As I have read, this may b...

How to design an efficient image buffer in C++?

I am trying to create a data buffer, more specifically, an image buffer, which will be shared among multiple modules. Those modules only reads from the buffer and don't communicate with each other at all. My difficulty is: 1.Large data size: larger than 10M per image, that means copying those data around for different threads is not...

Using shared_ptr in dll-interfaces.

I have an abstract class in my dll. class IBase { protected: virtual ~IBase() = 0; public: virtual void f() = 0; }; I want to get IBase in my exe-file which loads dll. First way is to create following function IBase * CreateInterface(); and to add the virtual function Release() in IBase. Second way is to create a...

Deleting a shared pointer (C++)

I have a pointer to a QScriptEngine that I'm passing through the overloaded class constructor of class Evaluator and assigns it to QScriptEngine *engine_ (class Property subclasses Evaluator, and calls this constructor of Evaluator, passing it an already allocated QScriptEngine). The constructor with no arguments creates the new QScriptE...

shared_ptr with templates

Hello! If I want to create a smart pointer to struct I do that: struct A { int value; }; typedef boost::shared_ptr<A> A_Ptr; So, I can write the following: A_Ptr pA0(new A); pA0->value = 123; But, If I have a template struct like that: template<typename T> struct B { T value; }; And I want to write the following: ...

c++ problem with polymorphism and vectors of pointers

Consider the following example code: class Foo { }; class Bar : public Foo { }; class FooCollection { protected: vector<shared_ptr<Foo> > d_foos; }; class BarCollection : public FooCollection { public: vector<shared_ptr<Bar> > &getBars() { // return d_foos won't do here... } }; I have a problem like this in ...

Problem with iterators for std::list of boost::shared_ptr

Hi, I'm having a problem with the following code: #include <list> #include <boost/shared_ptr.hpp> #include "Protocol/IMessage.hpp" template <typename HeaderType> class Connection { public: typedef IMessage<HeaderType> MessageType; typedef boost::shared_ptr<MessageType> MessagePointer; template <typename Handler>...

How to avoid memory leak with boost::shared_ptr?

Consider the following code. using boost::shared_ptr; struct B; struct A{ ~A() { std::cout << "~A" << std::endl; } shared_ptr<B> b; }; struct B { ~B() { std::cout << "~B" << std::endl; } shared_ptr<A> a; }; void main() { shared_ptr<A> a (new A); shared_ptr<B> b (new B); a->b = b; b->a = a; } There is ...

Detach a pointer from a shared_ptr?

A function of my interface returns a pointer to an object. The user is supposed to take ownership of that object. I do not want to return a Boost.shared_ptr, because I do not want to force clients to use boost. Internally however, I would like to store the pointer in a shared_ptr to prevent memory leaks in case of exceptions etc. There s...

const shared_ptr to shared_ptr

Hello How to convert shared_ptr that points to const object to shared_ptr that point to nonconst object. I am trying to do the following : boost::shared_ptr<const A> Ckk(new A(4)); boost::shared_ptr<A> kk=const_cast< boost::shared_ptr<A> > Ckk; But it doesnot work. ...