Jump to content
  • Advertisement
Sign in to follow this  
blackfe2010

Question on boost::shared_ptr

This topic is 2451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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? Edited by blackfe2010

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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? Edited by blackfe2010

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!