当我不希望它被调用时,C++ 析构函数被调用

C++ Destructor is being called when I don't expect it to be

我在一些代码中添加了一个析构函数,它似乎提前调用并导致了问题。我添加了一个调试语句来查看它被调用的位置,这让我更加困惑。我知道管理自己的内存不是最佳实践,但我想自己尝试一下。

这基本上是我的游戏对象class:

class GameObject
{
public:
int             xCoord = 0, yCoord = 0, prevX, prevY;
int             status = 0, weight = -1;
int             id = -1;

GameObject(CommandComponent* commands,
    PhysicsComponent* physics,
    GraphicsComponent* graphics)
    : commandComponent(commands),
    physicsComponent(physics),
    graphicsComponent(graphics)
{};

~GameObject()
{
    std::cout << "Destructor called " << id << std::endl;
    delete commandComponent;
    delete physicsComponent;
    delete graphicsComponent;
};

void update(World& world, int command, sf::Time dt)
{
    commandComponent->update(*this, world, command);
    physicsComponent->update(*this, world);
    graphicsComponent->update(*this, dt);
};

void update(World& world, int command)
{
    commandComponent->update(*this, world, command);
    physicsComponent->update(*this, world);
};

sf::Sprite draw()
{
    return *(graphicsComponent->draw());
};

void setCoords(int x, int y)
{
    prevX = xCoord;
    xCoord = x;
    prevY = yCoord;
    yCoord = y;
};

void setId(int newId)
{
    id = newId;
}


private:
    CommandComponent*           commandComponent    = NULL;
    GraphicsComponent*          graphicsComponent   = NULL;
    PhysicsComponent*           physicsComponent    = NULL;

};

这是 createPlayer 方法:

    GameObject* createPlayer(sf::Texture* text)
{
    return new GameObject(new PlayerCommandComponent(), new PlayerPhysicsComponent(), new PlayerGraphicsComponent(text));
};

这是我调用的一种方法,用于根据新对象是活动对象还是非活动对象将其添加到向量中,我还将其添加到数组中:

void World::addObject(GameObject object, int id, int type){
object.setId(id);

if (type == 0)
{
    inactiveObjects.push_back(object);
}
else if (type == 1)
{
    activeObjects.push_back(object);
}
}

最后,这是我的测试代码,它创建了游戏对象并调用了上面的函数,我在其中看到了调用析构函数的位置:

void World::test()
{
// Player
std::cout << "Starting to create id 0\n";
addObject((*createPlayer(&(mTextures.get(Textures::PlayerCharacter)))), 0, 1);
activeObjects.at(0).setCoords(3, 3);
activeObjects.at(0).weight = 10;
std::cout << "Created id 0\n";

// Test Objects
std::cout << "Starting to create id 1\n";
addObject((*createPlayer(&(mTextures.get(Textures::PlayerCharacter)))), 1, 1);
activeObjects.at(1).setCoords(3, 4);
activeObjects.at(1).weight = 7;
std::cout << "Created id 1\n";

addObject((*createPlayer(&(mTextures.get(Textures::PlayerCharacter)))), 2, 1);
activeObjects.at(2).setCoords(5, 4);
activeObjects.at(2).weight = 2;

addObject((*createPlayer(&(mTextures.get(Textures::Enemy)))), 3, 1);
activeObjects.at(3).setCoords(6, 6);
activeObjects.at(3).weight = -1;

addObject((*createPlayer(&(mTextures.get(Textures::Enemy)))), 4, 1);
activeObjects.at(4).setCoords(1, 1);
activeObjects.at(4).weight = 0;

std::cout << "Done Creating Test Objects\n";

我想我的主要问题是为什么会调用析构函数?我假设它与我在 createPlayer 方法中构建对象的方式有关,也许它在我 return 之后超出了范围,但我认为使用 new 关键字可以防止这种情况发生?我在这里很困惑。

这里有几件事在起作用。

GameObject* createPlayer(sf::Texture* text)

returns 一个动态分配的GameObjectThis could be done better,继续阅读 std::unique_ptr,但这里并没有严格意义上的错误。我提到它主要是为了指出 std::unique_ptr 并设置

addObject((*createPlayer(&(mTextures.get(Textures::PlayerCharacter)))), 0, 1);

因为这是事情开始出错的地方。当您发现使用 new 并取消引用并丢弃结果的代码时,您正在查看内存泄漏。您丢失了指向动态分配对象的指针,如果没有指针,几乎不可能再次找到分配,因此您可以 delete 它。

存储取消引用的对象将调用复制构造函数或赋值运算符,此时您需要考虑 The Rule of Three: If you need a to define a custom destructor, you probably need to define a custom assignment operator and a copy constructor. This is a standard example of when you need to observe the Rule of Three. What goes wrong is well-explained in the Rule of Three Link,因此在继续之前停下来阅读并理解它.不这样做意味着这个答案的其余部分将 nigh-useless 给你。

如果不牢牢把握 the Rule of Three and all of its friends,就无法编写出好的 non-trivial C++ 代码。

您可以通过更改

绕过此处的三法则
void World::addObject(GameObject object, int id, int type)

void World::addObject(GameObject * object, int id, int type)

并通过引用传递 object。这没什么用,因为

inactiveObjects.push_back(object);

需要一个对象,而不是指针。

你也可以改变它,但是你应该吗?当 std::vector 直接包含一个对象时,它绝对是最好的。指针导致指针追逐、缓存行为不佳,最终 suuuhhhfering。除非有令人信服的理由,否则不要存储指针。

如果这样做,请从头到尾管理指针 std::unique_ptr

我会做什么:

直接跳过三法则并转到 The Rule of Five

  1. 尽可能多地消除动态分配的变量,这样我就不需要为第 2 点做太多工作(如果有的话)。这意味着没有指向(或在)commandComponent、[=24 中的指针=] 和 graphicsComponent 如果可能的话。
  2. GameObject 以及 CommandComponentPhysicsComponentGraphicsComponent 添加移动构造函数和移动赋值运算符。让所有资源管理尽可能靠近资源。这允许您尽可能地保持更高级别 类 无知。如果 GraphicsComponent 知道如何复制和移动自己,那么 GameObject 就不需要知道如何移动它。这使您可以利用 The Rule of Zero,而零规则应该是您在所有 类.
  3. 中所追求的。
  4. 使用移动语义获得 GameObject,而不是 GameObject*createPlayeractiveObjectsinactiveObjects vector .
  5. 享受减少的内存管理负载。