shared-ptr

shared_ptr, subscription, destructor

I'm using Boost/shared_ptr pointers throughout my application. When the last reference to an object is released, shared_ptr will delete the object for me. The objects in the application subscribes to events in a central location of the application, similar to the observer/subscriber pattern. In the object destructors, the object will un...

C++: auto_ptr + forward decleration?

I have a class like this: class Inner; class Cont { public: Cont(); virtual ~Cont(); private: Inner* m_inner; }; in the .cpp, the constructor creates an instance of Inner with new and the destructor deletes it. This is working pretty well. Now I want to change this code to use auto_ptr so I write: class Inner; class Con...

Is it OK to use boost::shared ptr in DLL interface?

Is it valid to develop a DLL in C++ that returns boost shared pointers and uses them as parameters? So, is it ok to export functions like this? 1.) boost::shared_ptr<Connection> startConnection(); 2.) void sendToConnection(boost::shared_ptr<Connection> conn, byte* data, int len); In special: Does the reference count work across DLL b...

Finding boost::shared_ptr cyclic references

Is there any tips/tricks for finding cyclic references of shared_ptr's? This is an exmaple of what I'm trying to find - unfortunately I can't seem to find the loop in my code. struct A { boost::shared_ptr<C> anC; }; struct B { boost::shared_ptr<A> anA; }; struct C { boost::shared_ptr<B> anB; }; ...

boost, shared ptr Vs weak ptr? Which to use when?

Hello All, I am using boost shared pointer from considerable time in my project. Recently my fellow team mates have also started using weak pointers. I am not able to distinguish which to use when. Apart from this, what should I do if I want to convert weak ptr to shared ptr. Does putting a lock on weak ptr to create a shared ptr affec...

Why can't I access a public var using the getter?

having a file containing these statements: public: boost::shared_ptr<TBFControl::TbfCmdHandler> _tbfCmdHandlerPtr; // will be private later... boost::shared_ptr<TBFControl::TbfCmdHandler> getTBFCmdHandler() { return _tbfCmdHandlerPtr; } I can use it this way: boost::shared_ptr<TBFControl::TbfCmdHandler>myTbfCmdHandlerPtr( this->...

C++ smart pointer const correctness

I have a few containers in a class, for example, vector or map which contain shared_ptr's to objects living on the heap. For example template <typename T> class MyExample { public: private: vector<tr1::shared_ptr<T> > vec; map<tr1::shared_ptr<T> , int> h; }; I want to have a public interface of this class that sometimes returns sh...

Maintaining a std::set<boost::shared_ptr>

I'm writing a game and an accompanying engine in C++. The engine relies heavily on automation using a simple embedded scripting language. Scripts can create object classes, define event listeners on them, and produce instances from them. At present, an instance must be bound to a script-global identifier in order to preserve its existenc...

boost serialization issue with shared_ptr to std containers

Hi I am using boost/1.41.0, and the following code give me compilation error when I try to deserialize a shared_ptr. The serialize part it compiled successfully. Can someone advise me if this is a bug in my code or a general issue for boost? Thanks. Yanchao #include <iomanip> #include <iostream> #include <cstddef> // NULL #include <fst...

Avoiding indirect cyclic references when using shared_ptr and weak_ptr

I'm currently putting together an application that relies heavily on shared_ptr and everything looks good so far - I've done my homework and have a pretty good idea of some of the pitfalls of using shared_ptrs. One of the most recognised problems with shared_ptr is cyclic dependencies - these issues can be solved by storing weak_ptrs th...

Questions on usages of shared_ptr - C++

I have few questions on the best practices of using shared_ptr. Question 1 Is copying shared_ptr cheap? Or do I need to pass it as reference to my own helper functions and return as value? Something like, void init_fields(boost::shared_ptr<foo>& /*p_foo*/); void init_other_fields(boost::shared_ptr<foo>& /*p_foo*/); boost::shared_ptr<...

Are there any downsides with using make_shared to create a shared_ptr

Are there any downsides with using make_shared<T>() instead of using shared_ptr<T>(new T). Boost documentation states There have been repeated requests from users for a factory function that creates an object of a given type and returns a shared_ptr to it. Besides convenience and style, such a function is also exception s...

To get reference counting, do I have to clutter my APIs with shared_ptr?

I recently had the following memory bug, which is easy to spot here, but can be harder to detect in more complex code: class Foo : public IFoo { const Bar& bar_; public: Foo(const Bar& bar) : bar_(bar) { } void test() { // access bar_ here } }; int baz() { IFoo* foo = NULL; if(whatever) { Bar bar; foo = new Fo...

Iterating & containers of smart pointers

I have a container of smart pointers to mutable objects. I have to write two *for_each* loops, one for accessing the objects as read-only data and another for mutable data. The compiler is telling me that std::vector< boost::shared_ptr<Object> > is not the same as std::vector< boost::shared_ptr<const Object> >, note the const. Here ...

How to introduce boost::shared_ptr into an existing (large) C++ codebase?

Hi, I am currently trying to fix a few weaknesses in our code base by introducing the use of smart pointers. The code base is very large, and interlinked like a spider who's had one to many coffee's. I was wondering if people had tried the before and what their approach was. My first step has been to typedef classes, as follows. #ifn...

assigning shared ptrs (boost) in constructor , unit testing

I have a C++ class(inside a dll project) whose member variables are boost::shared_ptrs to objects of other classes. Is it better to assign them inside the class constructor or have a separate init() function which does that. I am assuming the default value of pointer to T inside boost::shared_ptr is NULL. So if I do nothing inside the ...

Getting shared_ptr refs to appear in doxygen collaboration diagrams.....

I've done enough Googling to know that if I have something like class SubObject { public: //blah blah blah }; class Aggregate { public: boost::shared_ptr<SubObject> m_ptr; }; I can get Doxygen to create the "correct" collaboration diagram if I have a dummy declaration like namespace boost { template<class T> class shared_ptr {...

How to get the pointer to a shared_ptr?

Hi, gents: I am now hacking an old C code, try to make it more C++/Boost style: there is a resource allocation function looks like: my_src_type* src; my_src_create(&src, ctx, topic, handle_src_event, NULL, NULL); i try to wrap src by a shared_ptr: shared_ptr<my_src_type> pSrc; I forgot to mention just now. I need to do this as a ...

shared_ptr requires complete type; cannot use it with lua_State*

Hello! I'm writing a C++/OOP wrapper for Lua. My code is: class LuaState { boost::shared_ptr<lua_State> L; LuaState(): L( luaL_newstate(), LuaState::CustomDeleter ) { } } The problem is lua_State is incomplete type and shared_ptr constructor requires complete type. And I need safe pointer sharing. (Funny thing bo...

What is the correct way to make sure user use shared_ptr and not regular ptr?

In my class the constructor is private and I added a static method "CreateMyClassPtr" that uses the constructor and returns a share_ptr of it. Is it the correct implementation? Do you think I even have to make sure that shared_ptr will be used? Should I maybe leave it to the user to decide? ...