简体   繁体   English

c++ pathfinder - 带指针的对象向量

[英]c++ pathfinder - vector of objects with pointers

I have a problem with destructor.我有析构函数的问题。 I have two classes: Map and Tile.我有两个类:Map 和 Tile。 They looks like that: (if code fragments below are not enough i'm adding github with this project with full code inside: https://github.com/TMalicki/PathFinder-algorithms )它们看起来像这样:(如果下面的代码片段不够用,我会在这个项目中添加 github,里面有完整的代码: https : //github.com/TMalicki/PathFinder-algorithms

class Tile
{
  private:
  Tile* parent;
  ...
  public:
  Tile(sf::Vector2f size = { 50.f ,50.f });
  Tile(const Tile& cTile);
  Tile& operator=(const Tile& aTile);
  ~Tile();
  ...
}

and

class Map
{
private:
vector<Tile*> board;
...
public:   
Map(sf::Vector2i numberOfTiles = { 10, 10 }, sf::Vector2f sizeOfTiles = { 
50.f, 50.f });
Map(const Map& cMap);
Map& operator=(const Map& aMap);
~Map();
...
}

They destructors looks like that:他们的析构函数看起来像这样:

Tile::~Tile()
{
    if (parent != nullptr)
    {
        delete parent;
        parent = nullptr;
    }
}

Map::~Map()
{
    for (int i = 0; i < board.size(); i++)
    {
        delete board[i];
        board[i] = nullptr;
    }
}

Code is used for shortest path search algorithm.代码用于最短路径搜索算法。 I have to store previous Tile so when algorithm find finish it can search backwards shortest path.我必须存储以前的 Tile,所以当算法找到完成时,它可以向后搜索最短路径。 I can add some more code if it;s needed.如果需要,我可以添加更多代码。 To sum up: Map object has vector of Tiles.总结一下: Map 对象具有 Tiles 向量。 And every Tile firstly is not pointing anything.并且每个 Tile 首先都没有指向任何东西。 But if it's used by algorithm than this exact Tile store position of previous one, and so on to the beginning.但是如果它被算法使用,而不是前一个的确切 Tile 存储位置,依此类推。 I know propably smart pointers can help me but i though it would be good for me to learn firstly how to do this maybe harder way.我知道可能的智能指针可以帮助我,但我认为首先学习如何以更难的方式做到这一点对我有好处。 I think that the problem is that (i don;t exactly know why) while deleting Map vector with Tiles i do not only delete exact Tile, but also somehow Tile who is parent of that Tile?我认为问题在于(我不知道为什么)在删除带有 Tiles 的 Map vector 时,我不仅删除了确切的 Tile,而且还以某种方式删除了该 Tile 的父级 Tile? And fe in the next iteration when it should delete that parent of previous iteration it cannot because it is already delete.而 fe 在下一次迭代中应该删除前一次迭代的父级时它不能,因为它已经被删除了。 I thought that after if statement inside Tile destructor it wont be delated if it already is.我认为在 Tile 析构函数中的 if 语句之后,如果它已经是,它就不会被延迟。 But it's not helping.但它没有帮助。

I've got exception thrown like that:我有这样的异常抛出:
在此处输入图片说明

And from debugger mode i have this:从调试器模式我有这个:

  • first iteration第一次迭代
    在此处输入图片说明

  • second iteration第二次迭代
    在此处输入图片说明

During analisation from debugger mode i've start thinking that destructor of exact tile delete firstly parent tile, and than himself.在调试器模式的分析过程中,我开始认为精确图块的析构函数首先删除父图块,而不是他自己。 And after that it jumps to line parent = nullptr inside Tile destructor.之后它跳转到 Tile 析构函数中的 parent = nullptr 行。 But somehow only child Tile is being set to nullptr.但不知何故,只有子 Tile 被设置为 nullptr。

The problem is likely that parent is not an owning pointer so it should not delete the parent.问题很可能是 parent 不是拥有指针,所以它不应该删除 parent。

Tile::~Tile() {
    if (parent != nullptr) {
        delete parent; <----------- here
        parent = nullptr;
    }
}

Otherwise when Map delete the tile, it has already been deleted.否则当 Map 删除 tile 时,它​​已经被删除了。 Removing this part of the code should solve the problem.删除这部分代码应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM