shared-ptr

data from a std::vector reference becomes corrupt after return from function call

I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in a compressed file. Here's the function I'm attempting to do that with: using std::tr1::shared_ptr; template<typename T> void loadSharedPtrVector(std::vector< shared_ptr<T> >& vect, TCODZip& zip) // the TCODZip is the compres...

How to retain local variable's value and revisit outside in C++

I use boost shared_ptr to wrap a pointer. However I can only get the correct value in test_shared_ptr(), while in main(), I get the wrong value. Expected output: 100 100 Actual output: 100 -572662307 It seems that the pointer becomes invalid in that case. Any correct approach to do the job? Than...

Portable way to use shared_ptr in GCC

GCC 4.1 uses the <tr1/memory> header and GCC 4.3 uses <memory> header, I need a portable way to use shared_ptr with GCC 4.3.2 and with GCC 4.2.1, is there any way to do that without checking GCC version macros or using external libraries like Boost ? ...

Soft (not: weak) references in C++ - Is it possible? Is there an implementation?

In C++ I'm using boost::shared_ptr and boost::weak_ptr to automatically delete objects that are no longer needed. I know these work with reference counting. In Java, memory is managed by a garbage collector, which consideres the built-in object references as strong, WeakReference as weak and SoftReference as something in between (may be...

How does shared_ptr<> safely allow casting to bool?

I was looking into how std::tr1::shared_ptr<> provides the ability to cast to bool. I've got caught out in the past when trying to create a smart pointer that can be casted to bool as the trivial solution, ie operator bool() { return m_Ptr!=0; } usually ends up being implicitly castable to the pointer type (presumably by type promo...

C++ basic pointer question

Hello! I have some shared pointer shared_ptr<T> pointer1(new T(1));. Now, in some other part of code I have an explicit copy of pointer2 (guess it would be stored in a std::map or some other container). Let's say that copy was done like map.insert(make_pair(key1, pointer1));. I am using that second copy only to precache some data and ...

C++ - pointer passing question

Does somebody have any idead on how to pass boost::shared_ptr - by value or by reference. On my platform (32bit) sizeof(shared_ptr) equals 8 bytes and it looks like I should pass them by reference, but maybe somebody has another opinion / did a profile / something like that? ...

Wrapping boost::shared_ptr in PHP using SWIG

If I use SWIG to wrap this C++ function: boost::shared_ptr<Client> Client::create() { return boost::shared_ptr<Client>(new Client()); } And then call it in PHP: $client = Client::create(); echo gettype($client); The type of $client is resource, not object, and so I cannot call Client methods. What are my options for wrapping t...

shared_ptr and slicing

Hi Someone I worked with once said that shared_ptr was unsafe and would slice when casting from a derived class to a base class (i.e. upcasting). For example if there were 2 classes A and B where B derived from A, then shared_ptr<A> a(new B) would slice. I pointed him to http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/shared_ptr...

C++ shared_ptr - attach to a new raw pointer?

I think I'm missing something simple here. I'm using Boost's shared_ptr. shared_ptr<Foo> pA(new Foo()); shared_ptr<Foo> pB(new Foo()); Now, I want to switch pB so it contains the contents of pA, decrementing the ref count of pB. How can I do this? ...

A std::tr1::shared_ptr for Objective C++ on iPhone ???

Hi, I am mostly a C++ developer, recently I am writing iPhone applications. The memory management on iPhone is OK to me, due to resource limitation, it's encouraged to use reference counters rather than deep copy. One annoying thing is I have to manage the reference counters by myself: alloc means counter = 1; retain means counter++, r...

shared_ptr by reference or by value?

When a function should take a boost::shared_ptr, are you passing it by const reference void foo(const boost::shared_ptr<T>& p) or by value void foo(boost::shared_ptr<T> p) ? I would prefer the first method because I suspect it to be faster. But is this really worth a though or are there any additional issues? Edit: Could you please g...

"address not from malloc()" error using electric fence

I've been writing a test case program to demonstrate a problem with a larger program of mine, and the test case has a bug that the original program does not. Here's the header file: // compiled with g++ -I/usr/local/bin/boost_1_43_0 -Wall -std=c++0x -g test.cpp #include <bitset> #include <boost/shared_ptr.hpp> #include <vector> typed...

Crazy C++ Vector iterator

I declare: typedef std::tr1::shared_ptr<ClassA> SharedPtr; And then: std::vector<SharedPtr> mList; And: typedef std::vector<SharedPtr>::iterator ListIterator; The return of mList.size() is 0, but when I use iterators, it iterates over the vector which is empty ! This is how I use the iterator: for(ListIterator it = mList.begin(...

How to add boost to my project?

I work on a cross-platform (Windows, Linux, Solaris) project. I want to use Boost's shared_ptr in this project. How can I install it, and redistribute it with the project to the customers? I don't have root permissions on Linux/Solaris, so I probably have to add Boost' sources to my sources, and build it together. Also, our version of...

testing if a shared_ptr is NULL

I have the following code snippet: std::vector< boost::shared_ptr<Foo> >::iterator it; it = returnsAnIterator(); // often, it will point to a shared_ptr that is NULL, and I want to test for that if(*it) { // do stuff } else // do other stuff Am I testing correctly? The boost docs say that a shared_ptr can be implicitly converted ...

handle management with boost::shared_ptr<>

I would like to use boost::shared_ptr<> to encapsulate the lifetime management of a handle. My handle and it's creation/destruction functions are declared like this: typedef const struct MYHANDLE__ FAR* MYHANDLE; void CloseMyHandle( MYHANDLE ); MYHANDLE CreateMyHandle(); Ideally, I would like to use the boost::shared_ptr<> like this ...

Using a static initializer function

In this contrived example, I have a static initialization function that is run at construction time. I'd like to know if this a legal way to initialize a_, b_, and c_. Or, is it too early in the construction process to use them this way? DWORD SomeInitializationFunction( HANDLE*, DWORD*, DWORD* ); class MyClass { public: MyClass()...

bind two arguments

Consider I have the following struct: struct IDirect3D { IDirect3D() : ref_count_(0) {} unsigned long Release() { return --ref_count_; } private: long ref_count_; ~IDirect3D() {} }; I want to use shared_ptr to it like in the followed code (minimal example): int main() { boost::shared_ptr<IDirect3D> ptr; IDire...

Wrapping linked lists in iterators

A set of APIs that I commonly use follow a linked-list pattern: struct SomeObject { const char* some_value; const char* some_other_value; SomeObject* next; } LONG GetObjectList( SomeObject** list ); void FreeObjectList( SomeObject* list ); This API is not mine and I cannot change it. So, I'd like to encapsulate their...