通过其成员变量删除类实例是否有效?

时间:2013-02-06 17:15:20

标签: android c++ ios

好的,所以我试图看看通过其成员变量删除一个类是否安全?成员变量有一个指向其包含类的指针。在m_objectPtr-> OnDestroy()之后,这在android上崩溃了;被叫。它适用于IOS我是一名同事告诉我的。我想知道这段代码是否有效。

class Object;
class ValueObject;
class SomeManager;

class DataInterface
{
public:
    virtual void OnDestroy(const ValueObject* value,void* data) = 0;
};

class destroyDelegate : public DataInterface
{
public:
    destroyDelegate(Object* obj) : m_objectPtr(obj)
    {
    }

    //...
    virtual void OnDestroy(const ValueObject* value,void* data)
    {
        m_objectPtr->OnDestroy();
    }
private:
    Object* m_objectPtr;
};

class Object
{
public:
    Object(SomeManager* manager,ValueObject* value) : 
    m_manager(manager), m_value(value), m_delegateObject(this)
    {
        // m_value calls OnDestroy when its being deleted. assume m_value is ref counted
        m_value->setDestroyCallBack(&m_delegateObject);
    }

    void OnDestroy()
    {
        m_manager->Destroy(this); // deletes this class and removes it from its std::list
    }
private:
    SomeManager* m_manager;
    ValueObject* m_value;
    destroyDelegate m_delegateObject;
};

// now some where in code m_value dies and calls on its member variable that inherits 
// DataInterface OnDestroy which chains down to deleting Object.

0 个答案:

没有答案