Question on boost::shared_ptr

Started by
14 comments, last by blackfe2010 11 years, 5 months ago
Hi,

I'm a beginner about boost.
And i try to write some code like:


#include "stdafx.h"
#include <boost/shared_ptr.hpp>
class A;
class B;
class A
{
public:
A() {};
boost::shared_ptr<B> a;
boost::shared_ptr<B> b;
};
class B
{
public:
B(){};
boost::shared_ptr<A> a;
boost::shared_ptr<A> b;
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b;
a.a=boost::shared_ptr<B>(&b);
a.b=boost::shared_ptr<B>(&b);
b.a=boost::shared_ptr<A>(&a);
b.b=boost::shared_ptr<A>(&a);
return 0;
}


This program is crash.
Who can tell me what's happend?
Advertisement
You should only use shared_ptr for dynamically allocated objects (allocated with new)! You are passing the address of objects created on the stack, that's why it crashes.
Hi
It works, thx.

And how to convert "this" pointer to shared_ptr?
I think you shouldn't, but can you give me an example of what you would like to do?
I want do something like this:

class Manager
{
public:
void addComponent(boost::shared_ptr<Component> comp)
{
comp->m_Manager=this;
}
};
class Component
{
public
boost::shared_ptr<Manager> m_Manager;
};

But how to let it work?
To that derive your class from boost::enable_shared_from_this and call shared_from_this().
oh~, thx. it works very well.
For completeness:

  • Because shared_ptr<> takes "ownership" of the passed pointer, it is incorrect to create two shared_ptr<>s from the same raw pointer
  • It is incorrect to create cyclical loops via shared_ptr (at least if you actually want to prevent memory leaks). The relationship would need to be parent/child, with the child holding a non-shared pointer (e.g. a weak_ptr<> or possibly a raw pointer if you are confident) to the parent.
Note that if you create multiple shared pointers to the same raw pointer (as noted above never to do) they will each hold their own reference count therefore deleting before the other is out of scope. That is what you should always construct shared pointers with either shared_from_this() in the class or make_shared() elsewhere.

For completeness:

  • Because shared_ptr<> takes "ownership" of the passed pointer, it is incorrect to create two shared_ptr<>s from the same raw pointer
  • It is incorrect to create cyclical loops via shared_ptr (at least if you actually want to prevent memory leaks). The relationship would need to be parent/child, with the child holding a non-shared pointer (e.g. a weak_ptr<> or possibly a raw pointer if you are confident) to the parent.


So you means two or more shared_ptr can not point to the same raw pointer?
But i found some description about shared_ptr like "Object ownership shared among multiple pointers."
How to understand it?

And actually, I want do something like:

class Object
{
public:
void addComponents(boost::shared_ptr<Component> comp)
{
m_components.push_back(comp);
}

vector< boost::shared_ptr<Componnet> > m_components;
};
class ObjectManager : public boost::enable_shared_from_this<ObjectManager>
{
public:
void addComponent(boost::shared_ptr<Component> comp)
{
comp->m_Manager=shared_from_this();
m_allComponents.push_back(comp);
m_allObjects[some_id].addComponents(comp);
}

vector< boost::shared_ptr<Component> >m_allComponents;
vector< boost::shared_ptr<Object> > m_allObjects;
};
class Component
{
public
boost::shared_ptr<ObjectManager> m_Manager;
};


Some Components in Object::m_components are point the same Component in ObjectManager::m_allcomponents.
So it is incorrect?

This topic is closed to new replies.

Advertisement