C ++中地牢爬行者的多类级别构造函数中的Valgrind内存泄漏

问题描述 投票:0回答:1

我在当前的项目中遇到了一个小内存泄漏,它使用c ++开发了一个小型dungeoncrawler。Valgrind向我展示了我在泄漏0 bytes in 1 blocks are definetly lost in loss record 1 of 38, Location level.cpp:6:0这行是我的构造函数定义。我似乎根本找不到问题,因为在阅读有关Valgrind的文章后,对该问题的唯一可能解释是,如果我创建了一个长度为0的数组,那么该数组将丢失,但是我的代码中不是这种情况。

关于该valgrind消息如何发生还有其他说明吗?

我不希望将代码上传到此处,因为该项目相当大,并且不确定代码是否会有所帮助。

编辑1:

Level::Level(const std::string& levelFileName)
{

    auto nodes = loadNodesFromLevelFile(levelFileName);
    std::vector<Node> switches;
    std::vector<Node> portals;

    for(const auto& node : nodes)
    {
        const auto nodeName = node.name;
        if(nodeName == "Map Information")
        {
            setHeight(node.get<int>("rows"));
            setWidth(node.get<int>("cols"));
            m_gameboard = new Tile**[m_height];

            for(auto row = 0; row < m_height; ++row)
            {
                m_gameboard[row] = new Tile*[m_width];
            }
        }
        if(nodeName == "Wall")
        {
            auto col = node.get<int>("col");
            auto row = node.get<int>("row");
            m_gameboard[row][col] = new Wall(row, col);
        }
        if(nodeName == "Floor")
        {
            auto col = node.get<int>("col");
            auto row = node.get<int>("row");
            m_gameboard[row][col] = new Floor(row, col);
        }
        if(nodeName == "Door")
        {
            auto col = node.get<int>("col");
            auto row = node.get<int>("row");
            m_gameboard[row][col] = new Door(row, col);
        }
        if(nodeName == "Portal")
        {
            auto col = node.get<int>("col");
            auto row = node.get<int>("row");
            m_gameboard[row][col] = new Portal(row, col, nullptr);
            portals.push_back(node);
        }
        if(nodeName == "Switch")
        {
            switches.push_back(node);
        }
        if(nodeName == "Character")
        {
            auto col = node.get<int>("col");
            auto row = node.get<int>("row");
            auto icon = node.get<char>("icon");
            placeCharacter(new Character(icon), row, col);
        }
    }
    for(const auto& portalNode : portals)
    {
        const auto row = portalNode.get<int>("row");
        const auto col = portalNode.get<int>("col");
        const auto destRow = portalNode.get<int>("destrow");
        const auto destCol = portalNode.get<int>("destcol");

        auto* portal = static_cast<Portal*>(m_gameboard[row][col]);
        auto* destination = static_cast<Portal*>(m_gameboard[destRow][destCol]);
        portal->setDestination(destination);
    }



    for(const auto& s : switches)
    {
        const auto row = s.get<int>("row");
        const auto col = s.get<int>("col");
        const auto destRows = s.get<std::vector<int>>("destrows");
        const auto destCols = s.get<std::vector<int>>("destcols");

        auto* sw = new Switch(row, col);
        m_gameboard[row][col] = sw;
        for(ulong i = 0; i < destRows.size(); ++i)
        {
            auto* target = m_gameboard[destRows[i]][destCols[i]];
            sw->attach(dynamic_cast<Door*>(target));
        }
    }
}

这是我级别的构造函数(从第6行开始。)>

我在当前的项目中遇到了一个小内存泄漏,它使用c ++开发了一个小型dungeoncrawler。 Valgrind向我展示了我在1个块中泄漏了0个字节,这些记录在38条丢失记录1中明确丢失,...

c++ memory-leaks valgrind
1个回答
0
投票

Okey,到目前为止,我已经弄清楚了为什么我的程序丢失了字节。

© www.soinside.com 2019 - 2024. All rights reserved.