正如标题声称的那样,我得到了一个输出:
The thread 0x1a34 has exited with code 0 (0x0).
The thread 0x1f94 has exited with code 0 (0x0).
'SecondPersonShooter.exe' (Win32): Unloaded 'C:\Windows\System32\powrprof.dll'
The thread 0xbf8 has exited with code 0 (0x0).
The thread 0x1dbc has exited with code 0 (0x0).
HEAP[SecondPersonShooter.exe]: HEAP: Free Heap block 48d0e10 modified at 48db40c after it was freed
SecondPersonShooter.exe has triggered a breakpoint.
但我没有分配任何我怀疑它的地方。我只是指向内存,没有动态分配。
代码:
node* currentNode = nullptr;
node* targetNode = nullptr;
std::vector<node*> path;
int valueOrigin = 0;
int valueTarget = 0;
float oldDistanceOrigin = 1000.f; // arbitrary numbers, just so theres a base point for distances
float oldDistanceTarget = 1000.f;
for (int i = 0; i < allNodes.size(); i++)
{
float distance = sqrt(std::pow(pos.x - allNodes[i].position.x, 2) + std::pow(pos.y - allNodes[i].position.y, 2));
if (distance < oldDistanceOrigin)
{
oldDistanceOrigin = distance;
valueOrigin = i;
}
distance = sqrt(std::pow(targetPos.x - allNodes[i].position.x, 2) + std::pow(targetPos.y - allNodes[i].position.y, 2));
if (distance < oldDistanceTarget)
{
oldDistanceTarget = distance;
valueTarget = i;
}
}
bool pathFound = false;
currentNode = &allNodes[valueOrigin];
currentNode->explored = true;
targetNode = &allNodes[valueTarget];
while (!pathFound)
{
for (int j = 0; j < currentNode->neighbors.size(); j++)
{
currentNode->explored = false;
currentNode->neighbors[j]->gCost = currentNode->gCost + 10;
currentNode->neighbors[j]->hCost = ( targetPos.x - pos.x ) / 64; // 64 is the size of a node
currentNode->neighbors[j]->hCost += ( targetPos.y - pos.y ) / 64;
currentNode->neighbors[j]->hCost *= 10;
if (currentNode->neighbors[j]->hCost < 0)
currentNode->neighbors[j]->hCost *= -1;
currentNode->neighbors[j]->fCost = currentNode->neighbors[j]->gCost + currentNode->neighbors[j]->hCost;
}
node* selection = currentNode;
for (int i = 0; i < currentNode->neighbors.size(); i++)
{
if (currentNode->neighbors[i]->fCost < selection->fCost)
{
selection = currentNode->neighbors[i];
}
}
if (selection != targetNode)
{
currentNode = selection;
path.push_back(currentNode);
}
else
{
pathFound = true;
}
}
std::vector<sf::Vector2f> pathPoints;
for (int i = 0; i < path.size(); i++)
{
pathPoints.push_back(path[i]->position);
path[i] = nullptr;
}
cleanup();
path.clear();
currentNode = nullptr;
targetNode = nullptr;
return pathPoints;
节点:
struct node
{
sf::Vector2f position;
std::vector<node*> neighbors;
bool explored;
int gCost;
int hCost;
int fCost;
};
很抱歉这么大的一块,但我怀疑这不是真正的罪魁祸首。但是,注释掉代码将导致程序运行。所以我不知道。 对不起,如果这是一个简单的问题,但我花了太长时间试图调试这个
谢谢你的时间!
修改1:
我发现,一旦我越过构造函数(之前邻居的所有数据都很好),一旦我进入函数发布,所有数据都将是垃圾。 像这样: 构造函数中的数据:
position= x=1664.00000 y=1920.00000
构造函数后的数据:
position = x=-1.58839967e+038 y=-1.58839967e+038
此编号对于所有邻居指针节点都是相同的。是否有任何特殊原因导致指针突然“变坏”?
答案 0 :(得分:1)
如果allNodes
为空,您将很容易损坏您的堆:
currentNode = &allNodes[valueOrigin]; // valueOrigin will be 0 for that case
currentNode->explored = true; // here you will be touching heap memory (where the vector element would have been) where no element actually lies