在C ++向量中的push_back()时访问冲突写入位置?

时间:2013-11-16 14:51:51

标签: c++ class vector stl

背景资讯

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

更多积分

  • 退出程序时,我一直收到“堆损坏”错误,但我没有修复它。
  • 在编译bug版本之前,我对源码进行的唯一编辑是我向另一个公共类添加了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();
};

最小化“Object Handler.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()处抛出错误。它第一次工作。 This is what my console outputs

these are all of the warnings that are thrown compile-time(在我遇到这些问题之前没有出现过这些问题)。

1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(2156): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(1923): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(1924): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\objecthandler.cpp(261): warning C4715: 'ObjectHandler::getTexture' : not all control paths return a value
1>c:\users\brady\dropbox\dwell project\project\client\client\networkmanager.cpp(41): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\actionhandler.cpp(226): warning C4715: 'ActionHandler::getActionName' : not all control paths return a value
1>c:\users\brady\dropbox\dwell project\project\client\client\game.cpp(19): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\menu.cpp(487): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\menu.cpp(585): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(1858): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(1859): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(607): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(612): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(837): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(838): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(848): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\gamegui.cpp(855): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\game.cpp(632): warning C4789: destination of memory copy is too small
1>c:\users\brady\dropbox\dwell project\project\client\client\networkmanager.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 11/16/2013]我刚刚在调试模式下运行它,它指向我的Game类的开头,而不是ObjectHandler类。 Game类是我通过添加2个新向量编辑的类。有趣。它仍然会抛出相同的异常。

更新

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

1 个答案:

答案 0 :(得分:1)

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

故事的道德: 始终在破坏构建之前修复堆错误的损坏!