• Create Account

### #Actualblackfe2010

Posted 01 November 2012 - 05:54 PM

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:
{
m_components.push_back(comp);
}

vector< boost::shared_ptr<Componnet> > m_components;
};
class ObjectManager : public boost::enable_shared_from_this<ObjectManager>
{
public:
{
comp->m_Manager=shared_from_this();
m_allComponents.push_back(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?

### #4blackfe2010

Posted 01 November 2012 - 05:53 PM

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:
{
m_components.push_back(comp);
}

vector< boost::shared_ptr<Componnet> > m_components;
};
class ObjectManager : public boost::enable_shared_from_this<ObjectManager>
{
public:
{
comp->m_Manager=shared_from_this();
m_allComponents.push_back(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?

### #3blackfe2010

Posted 01 November 2012 - 05:51 PM

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:
vector< boost::shared_ptr<Componnet> > m_components;
};
class ObjectManager : public boost::enable_shared_from_this<ObjectManager>
{
public:
{
comp->m_Manager=shared_from_this();
}

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?

### #2blackfe2010

Posted 01 November 2012 - 05:47 PM

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:
vector< boost::shared_ptr<Componnet> > m_components;
};
class ObjectManager
{
public:
vector< boost::shared_ptr<Component> >m_allComponents;
vector< boost::shared_ptr<Object> > m_allObjects;
};
class Component
{
};


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

### #1blackfe2010

Posted 01 November 2012 - 05:46 PM

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:
vector< boost::shared_ptr<Componnet> > m_components;
};
class ObjectManager
{
public:
vector< boost::shared_ptr<Component> >m_allComponents;
vector< boost::shared_ptr<Object> > m_allObjects;
};
class Component
{
};


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

PARTNERS