C ++ - 在执行析构函数之前销毁对象

时间:2014-04-01 21:52:41

标签: c++ destructor

我有一个类,它有一个指向动态分配实例的指针的二维向量(wallGameObjects),我想在调用析构函数时删除它,但是当析构函数执行时,向量已被销毁。

析构函数调用removeMaze():

MazeSceneBuilder::~MazeSceneBuilder()
{
    removeMaze();

    mSceneManager->destroySceneNode(mazeNode);
}

对象的成员变量已经消失了:

void MazeSceneBuilder::removeMaze()
{
    // Remove walls
    for (unsigned int x = 0; x < mazeWidth; x++)
    {
        for (unsigned int z = 0; z < mazeWidth; z++)
        {
            if (wallGameObjects[x][z] != nullptr)
            {
                mGameObjectManager->removeGameObject(wallGameObjects[x][z]);
            }
        }
    }

    // remove ground
    mGameObjectManager->removeGameObject(groundGameObject);

    // Destroy collision shapes
    destroyCollisionShapes();
}

“mGameObjectManager-&gt; removeGameObject(groundGameObject);”行上的断点显示了这个:

this    0x00000000 <NULL>   MazeSceneBuilder *

对我来说没有意义。我试图在对象被销毁之前调用“removeMaze()”并且它工作得很好。 在析构函数执行之前,对象的成员是否想要离开?

编辑:

根据要求,这是MultiplayerMaze的析构函数:

MultiplayerMaze::~MultiplayerMaze()
{
    destroyPhysicsWorld();
}

destroyPhysicsWorld:

void MultiplayerMaze::destroyPhysicsWorld()
{
    delete dynamicsWorld;
    delete solver;
    delete collisionConfiguration;
    delete dispatcher;
    delete broadphase;
}

编辑2:

MultiplayerMaze类定义:

class MultiplayerMaze : public BaseApplication
{
public:
    MultiplayerMaze();
    virtual ~MultiplayerMaze();

protected:
    virtual void createScene();
    virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
    virtual bool configure();

    virtual void createPhysicsWorld();
    virtual void destroyPhysicsWorld();

    // Physics members
    btBroadphaseInterface* broadphase;
    btDefaultCollisionConfiguration* collisionConfiguration;
    btCollisionDispatcher* dispatcher;
    btSequentialImpulseConstraintSolver* solver;
    btDiscreteDynamicsWorld* dynamicsWorld;

    GameObjectManager gameObjectManager;

    MazeSceneBuilder mazeSceneBuilder;
    MazeGenerator mazeGenerator;
};

MazeSceneBuilder类定义:

class MazeSceneBuilder
{
protected:
    std::vector<std::vector<bool>> mMaze;
    unsigned int mazeWidth, mazeHeight;
    float mMazeScale;

    GameObjectManager* mGameObjectManager;
    Ogre::SceneManager* mSceneManager;
    Ogre::String mWallEntity;
    Ogre::String mGroundMaterial;
    Ogre::Entity* mGround;
    Ogre::SceneNode* mazeNode;
    btCollisionShape* wallCollisionShape;
    btCollisionShape* collisionPlanes[5];
    GameObject* groundGameObject;
    std::vector<std::vector<GameObject*>> wallGameObjects;

    void setupGround();     // Creates a plane entity using the ground materiala and the maze size.
    void createCollisionShapes();       // Create collision shapes for the wall, ground, and bounding planes.
    void destroyCollisionShapes();
    void DestroyAllAttachedMovableObjects(Ogre::SceneNode* sceneNode);

public:
    MazeSceneBuilder();
    MazeSceneBuilder(GameObjectManager* gameObjectManager);
    ~MazeSceneBuilder();

    void setGameObjectManager(GameObjectManager* gameObjectManager);
    bool setMaze(std::vector<std::vector<bool>> maze);      // Returns false if the provided maze was invalid.
    void setMazeScale(float scale);
    void setWallEntity(Ogre::String entityName);        // Must be a square cuboid of 1x2x1
    void setGroundMaterial(Ogre::String materialName);  // Should be a tiled square texture.
    void buildMaze();
    void updateMaze(const std::vector<std::vector<bool>>& maze);
    void removeMaze();
};

1 个答案:

答案 0 :(得分:1)

这可能与析构函数无关,但请查看:

// Remove walls
for (unsigned int x = 0; x < mazeWidth; x++)
{
    for (unsigned int z = 0; z < mazeWidth; z++)
    {
        if (wallGameObjects[x][z] != nullptr)
        {
            mGameObjectManager->removeGameObject(wallGameObjects[x][z]);
        }
    }
}

您使用mazeWidth两次。你不应该使用mazeHeight吗?如果迷宫不是方形,你最终可能会在wallGameObjects中使用一些未初始化的指针,副作用无法预料,特别是如果这些对象有一个破坏其他对象的析构函数,等等。

由于课程很复杂,我建议尽可能使用std::unique_ptr<T>来建模所有权。它可以帮助您跟踪错误。