msvcp140d.dll如何导致整数比较错误?

时间:2017-08-11 17:56:53

标签: c++ dll visual-studio-2015 directx-11

我正在使用VS2015上的DirectX11应用程序。最近我遇到了这个msvcp140d.dll库导致的运行时错误。发生错误时的调用堆栈如下所示: enter image description here

生成错误的代码如下所示: enter image description here

据我所知,变量的值是可以接受的,所以我不确定是什么导致这个msvcp140d.dll在这行代码中断。这是否意味着这个dll可能不正确?或者别的什么出错了?

有错误的函数是我对A *算法的实现。

vector<pair<int, int>> NavigationManager::pathFinding(int startX, int startY, int endX, int endY) const{
vector<pair<int, int>> path;
if (startX == endX&&startY == endY)
    return path;

Node ***nodeMap = new Node**[mapHeight];
for (int i = 0; i<mapHeight; ++i)
{
    nodeMap[i] = new Node*[mapWidth];
    for (int j = 0; j<mapWidth; ++j)
    {
        if (map[i][j] != 1) {
            nodeMap[i][j] = new Node(j, i);
            nodeMap[i][j]->hValue = (abs(endX - j) + abs(endY - i)) * 10;
        }
        else
            nodeMap[i][j] = nullptr;
    }
}

auto comp = [](Node *a, Node *b) {return (a->gValue + a->hValue) > (b->gValue + a->hValue); };
priority_queue<Node*, vector<Node*>, decltype(comp)> pq(comp);
vector<Node*> v;
int currentX, currentY, currentGValue;
Node *currentParent = nullptr;
do
{
    if (pq.empty())
    {
        currentX = startX;
        currentY = startY;
        currentGValue = 0;
    }
    else
    {
        currentParent = pq.top();
        currentX = currentParent->x;
        currentY = currentParent->y;
        currentGValue = currentParent->gValue;
        v.push_back(currentParent);
        pq.pop();
    }
    if (currentX == endX&&currentY == endY)
        break;

    if (currentX<mapWidth - 1 && map[currentY][currentX + 1] != 1)
    {
        if (nodeMap[currentY][currentX + 1]->gValue == 0)
        {
            nodeMap[currentY][currentX + 1]->gValue = currentGValue + 10;
            nodeMap[currentY][currentX + 1]->parent = currentParent;
            pq.push(nodeMap[currentY][currentX + 1]);
        }
        else if (currentGValue + 10<nodeMap[currentY][currentX + 1]->gValue)
        {
            nodeMap[currentY][currentX + 1]->gValue = currentGValue + 10;
            nodeMap[currentY][currentX + 1]->parent = currentParent;
        }
    }

    if (currentX<mapWidth - 1 && map[currentY][currentX + 1] != 1 && currentY<mapHeight - 1 && map[currentY + 1][currentX] != 1 && map[currentY + 1][currentX + 1] != 1)
    {
        if (nodeMap[currentY + 1][currentX + 1]->gValue == 0)
        {
            nodeMap[currentY + 1][currentX + 1]->gValue = currentGValue + 14;
            nodeMap[currentY + 1][currentX + 1]->parent = currentParent;
            pq.push(nodeMap[currentY + 1][currentX + 1]);
        }
        else if (currentGValue + 14<nodeMap[currentY + 1][currentX + 1]->gValue)
        {
            nodeMap[currentY + 1][currentX + 1]->gValue = currentGValue + 14;
            nodeMap[currentY + 1][currentX + 1]->parent = currentParent;
        }
    }

    if (currentY<mapHeight - 1 && map[currentY + 1][currentX] != 1)
    {
        if (nodeMap[currentY + 1][currentX]->gValue == 0)
        {
            nodeMap[currentY + 1][currentX]->gValue = currentGValue + 10;
            nodeMap[currentY + 1][currentX]->parent = currentParent;
            pq.push(nodeMap[currentY + 1][currentX]);
        }
        else if (currentGValue + 10<nodeMap[currentY + 1][currentX]->gValue)
        {
            nodeMap[currentY + 1][currentX]->gValue = currentGValue + 10;
            nodeMap[currentY + 1][currentX]->parent = currentParent;
        }
    }

    if (currentX>0 && map[currentY][currentX - 1] != 1 && currentY<mapHeight - 1 && map[currentY + 1][currentX] != 1 && map[currentY + 1][currentX - 1] != 1)
    {
        if (nodeMap[currentY + 1][currentX - 1]->gValue == 0)
        {
            nodeMap[currentY + 1][currentX - 1]->gValue = currentGValue + 14;
            nodeMap[currentY + 1][currentX - 1]->parent = currentParent;
            pq.push(nodeMap[currentY + 1][currentX - 1]);
        }
        else if (currentGValue + 14<nodeMap[currentY + 1][currentX - 1]->gValue)
        {
            nodeMap[currentY + 1][currentX - 1]->gValue = currentGValue + 14;
            nodeMap[currentY + 1][currentX - 1]->parent = currentParent;
        }
    }

    if (currentX>0 && map[currentY][currentX - 1] != 1)
    {
        if (nodeMap[currentY][currentX - 1]->gValue == 0)
        {
            nodeMap[currentY][currentX - 1]->gValue = currentGValue + 10;
            nodeMap[currentY][currentX - 1]->parent = currentParent;
            pq.push(nodeMap[currentY][currentX - 1]);
        }
        else if (currentGValue + 10<nodeMap[currentY][currentX - 1]->gValue)
        {
            nodeMap[currentY][currentX - 1]->gValue = currentGValue + 10;
            nodeMap[currentY][currentX - 1]->parent = currentParent;
        }
    }

    if (currentX>0 && map[currentY][currentX - 1] != 1 && currentY>0 && map[currentY - 1][currentX] != 1 && map[currentY - 1][currentX - 1] != 1)
    {
        if (nodeMap[currentY - 1][currentX - 1]->gValue == 0)
        {
            nodeMap[currentY - 1][currentX - 1]->gValue = currentGValue + 14;
            nodeMap[currentY - 1][currentX - 1]->parent = currentParent;
            pq.push(nodeMap[currentY - 1][currentX - 1]);
        }
        else if (currentGValue + 14<nodeMap[currentY - 1][currentX - 1]->gValue)
        {
            nodeMap[currentY - 1][currentX - 1]->gValue = currentGValue + 14;
            nodeMap[currentY - 1][currentX - 1]->parent = currentParent;
        }
    }

    if (currentY>0 && map[currentY - 1][currentX] != 1)
    {
        if (nodeMap[currentY - 1][currentX]->gValue == 0)
        {
            nodeMap[currentY - 1][currentX]->gValue = currentGValue + 10;
            nodeMap[currentY - 1][currentX]->parent = currentParent;
            pq.push(nodeMap[currentY - 1][currentX]);
        }
        else if (currentGValue + 10<nodeMap[currentY - 1][currentX]->gValue)
        {
            nodeMap[currentY - 1][currentX]->gValue = currentGValue + 10;
            nodeMap[currentY - 1][currentX]->parent = currentParent;
        }
    }

    if (currentX<mapWidth - 1 && map[currentY][currentX + 1] != 1 && currentY>0 && map[currentY - 1][currentX] != 1 && map[currentY - 1][currentX + 1] != 1)
    {
        if (nodeMap[currentY - 1][currentX + 1]->gValue == 0)
        {
            nodeMap[currentY - 1][currentX + 1]->gValue = currentGValue + 14;
            nodeMap[currentY - 1][currentX + 1]->parent = currentParent;
            pq.push(nodeMap[currentY - 1][currentX + 1]);
        }
        else if (currentGValue + 14<nodeMap[currentY - 1][currentX + 1]->gValue)
        {
            nodeMap[currentY - 1][currentX + 1]->gValue = currentGValue + 14;
            nodeMap[currentY - 1][currentX + 1]->parent = currentParent;
        }
    }
} while (!pq.empty());

if (v.size() == 0 || v[v.size() - 1]->x != endX || v[v.size() - 1]->y != endY)
    return path;

stack<pair<int, int>> s;
if (!v.empty())
{
    Node *destination = v[v.size() - 1];
    while (destination)
    {
        s.push(pair<int, int>(destination->x, destination->y));
        destination = destination->parent;
    }
}
while (!s.empty())
{
    path.push_back(s.top());
    s.pop();
}
for (int i = 0; i<mapHeight; ++i)
{
    for (int j = 0; j<mapWidth; ++j)
    {
        delete nodeMap[i][j];
    }
    delete[] nodeMap[i];
}
delete[] nodeMap;

return path;

}

1 个答案:

答案 0 :(得分:0)

您正在更改优先级队列中对象的排序键。

查看典型案例,如果节点的gValue为零,则为其分配gValue并将其添加(指向)优先级队列。稍后,当节点仍在优先级队列中时,您可能会更改gValue。这可能导致排序顺序全部搞砸,打破容器依赖的排序,以及谁知道接下来会发生什么。这就是priority_queue::top()返回const引用的原因。

如果您需要更改pq中已有的元素,则需要将其删除,更改值,然后重新添加。