在C++向量中push_back() 时访问违规写入位置

Access violation writing location when push_back() in a C++ vector?

本文关键字:访问 位置 向量 C++ push back      更新时间:2023-10-16

>背景信息

当抛出"访问冲突写入位置0x00000008"时,我正在编辑源代码的完全不同的部分。对象加载之前一直在工作,我在错误编译之前修改的部分在一个完全不同的类中。

更多积分

  • 退出程序时,我不断收到"堆损坏"错误,但我没有修复它。
  • 在编译错误版本之前,我对源代码所做的唯一编辑是我在另一个与对象加载无关的公共类中添加了 2 个新向量。是内存不足还是什么?

最小化的"ObjectHandler.hpp"

#include "Object.hpp"
#include "SFML/System.hpp"
#include "SFML/Network.hpp"
struct objtex
{
    sf::Int32 id;
    sf::Texture* tx;
};
class ObjectHandler
{
    private:
        std::vector< std::vector < std::vector< Object* > > > objects;
        std::vector<objtex> textures;
    public:
        void loadTextures();
};

最小化的"对象处理程序.cpp"

void ObjectHandler::loadTextures()
{
    bool stillLoading = true;
    sf::Int32 id = 0;
    while(stillLoading)
    {
        std::stringstream ss;
        ss << "rsc/Objects/" << id << ".png";
        sf::Image i;
        if(i.loadFromFile(ss.str()))
        {
            i.createMaskFromColor(sf::Color(255, 0, 255));
            objtex o;
            o.id = id;
            o.tx = new sf::Texture;
            o.tx->loadFromImage(i);
            std::cout << "OOO";
            textures.push_back(o);      //  <<< THE PROBLEM >>>
            std::cout << "PPP";
            id += 1;
        }
        else
        {
            stillLoading = false;
            std::cout << "exited";
        }
    }
}

编译结果

每当我从 Main.cpp调用 loadTextures() 时,它在第二次通过循环后会在 textures.push_back() 处抛出错误。它第一次工作。这是我的控制台输出的内容。

这些是编译时抛出的所有警告(在我遇到这些问题之前没有出现)。

1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(2156): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(1923): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(1924): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientobjecthandler.cpp(261): warning C4715: 'ObjectHandler::getTexture' : not all control paths return a value
1>c:usersbradydropboxdwell projectprojectclientclientnetworkmanager.cpp(41): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientactionhandler.cpp(226): warning C4715: 'ActionHandler::getActionName' : not all control paths return a value
1>c:usersbradydropboxdwell projectprojectclientclientgame.cpp(19): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientmenu.cpp(487): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientmenu.cpp(585): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(1858): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(1859): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(607): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(612): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(837): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(838): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(848): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgamegui.cpp(855): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientgame.cpp(632): warning C4789: destination of memory copy is too small
1>c:usersbradydropboxdwell projectprojectclientclientnetworkmanager.cpp(879): warning C4789: destination of memory copy is too small

所有这些警告都会将我带到代码中的随机位置 - 这些都与内存分配无关。都是完全随机的。例如:

Game::Game()
{
    windowPos = gl::Win.getPosition();
    sprinting = false;
    hit = false;
    hasFocus = true;
    dead = false;
    isChiefStaff = false;
    drawGUI = true;           //  <<<  WARNING POINTS HERE  >>> 
}

提前谢谢你!

更新

    [11
  • :04 2013/11/16] 我只是在调试模式下运行它,它指向我的游戏类的开头,而不是对象处理程序类的开头。游戏类是我通过添加 2 个新向量编辑的类。有趣。它仍然会抛出相同的异常。

更新

  • [7:02 2013/11/16] 我修复了它。我重建了解决方案,并在删除后将所有指针设置为 NULL。这修复了堆上发生的所有损坏。感谢那些帮助过的人。

我修复了它。我重建了解决方案,并在删除后将所有指针设置为 NULL。这修复了堆上发生的所有损坏。感谢那些帮助过的人。

故事的寓意:始终在堆错误损坏您的构建之前修复它们损坏!