C++ 在C+;中的push#back()时访问违反写入位置+;矢量?
背景信息 当抛出“访问冲突写入位置0x00000008”时,我正在编辑源代码的一个完全不同的部分。对象加载以前一直在工作,而在有缺陷的编译之前修改的第1部分在一个完全不同的类中 更多积分C++ 在C+;中的push#back()时访问违反写入位置+;矢量?,c++,class,vector,stl,C++,Class,Vector,Stl,背景信息 当抛出“访问冲突写入位置0x00000008”时,我正在编辑源代码的一个完全不同的部分。对象加载以前一直在工作,而在有缺陷的编译之前修改的第1部分在一个完全不同的类中 更多积分 退出程序时,我不断收到“堆损坏”错误,但我没有修复它 在编译buggy版本之前,我对源代码所做的唯一编辑是,我向另一个与对象加载无关的公共类添加了2个新向量。是内存不足还是怎么了 最小化的“ObjectHandler.hpp” 所有这些警告都会将我带到代码中的随机位置,而这些位置都与内存分配无关。都是完全随
- 退出程序时,我不断收到“堆损坏”错误,但我没有修复它李>
- 在编译buggy版本之前,我对源代码所做的唯一编辑是,我向另一个与对象加载无关的公共类添加了2个新向量。是内存不足还是怎么了
Game::Game()
{
windowPos = gl::Win.getPosition();
sprinting = false;
hit = false;
hasFocus = true;
dead = false;
isChiefStaff = false;
drawGUI = true; // <<< WARNING POINTS HERE >>>
}
Game::Game()
{
windowPos=gl::Win.getPosition();
短跑=错误;
命中=错误;
hasFocus=true;
死=假;
isChiefStaff=假;
drawGUI=true;//>
}
提前谢谢你
更新
- [11:04 11/16/2013]我只是在调试模式下运行它,它指向我的游戏类的开头,而不是ObjectHandler类。游戏类是我通过添加2个新向量编辑的类。有趣。它仍然抛出相同的异常
- [7:02 11/16/2013]我修好了。我重新构建了解决方案,并在删除后将所有指针设置为NULL。这修复了堆中发生的所有损坏。感谢那些帮助过我的人
- 我把它修好了。我重新构建了解决方案,并在删除后将所有指针设置为NULL。这修复了堆中发生的所有损坏。感谢那些帮助过我的人
故事的寓意:
总是在堆错误损坏您的构建之前修复它们的损坏 我把它修好了。我重新构建了解决方案,并在删除后将所有指针设置为NULL。这修复了堆中发生的所有损坏。感谢那些帮助过我的人 故事的寓意:
总是在堆错误损坏您的构建之前修复它们的损坏
objtex
携带原始资源-指向sf::Texture
的指针。它需要一个复制构造函数、赋值运算符和析构函数。搜索三个规则。在程序中的某个地方,您正在覆盖堆分配的缓冲区。这会破坏堆vector
将在添加项时在堆上分配内存。所以你的问题是,你有堆腐败的地方。堆损坏通常不会在发生时导致崩溃,但下次有东西访问堆时会导致崩溃。修复堆损坏。您应该将编译器警告作为文本复制到问题中,而不是发布屏幕转储。您还可以标记代码,这些代码是警告在此处抱怨的行。如果不是,您可以编辑您的问题并将代码放在这里,这样我们就可以理解您在这些行中所做的事情。@drescherjm好的,我刚刚用一些更多的信息编辑了OP。@Jungle-如果您覆盖缓冲区的边界,您将得到C中所谓的“未定义行为”。由于行为未定义,您的代码可以正常工作,立即崩溃,稍后崩溃,或者导致恶魔从您的鼻子里飞出来。如果你的程序有未定义的行为,你不能对它进行推理,因为你不知道它会做什么。是的,这可能是你的问题。objtex
携带一个原始资源-指向sf::Texture
的指针。它需要一个复制构造函数、赋值运算符和析构函数。搜索三个规则。在程序中的某个地方,您正在覆盖堆分配的缓冲区。这会破坏堆vector
将在添加项时在堆上分配内存。所以你的问题是,你有堆腐败的地方。堆损坏通常不会在发生时导致崩溃,但下次有东西访问堆时会导致崩溃。修复堆损坏。您应该将编译器警告作为文本复制到问题中,而不是发布屏幕转储。您还可以标记代码,这些代码是警告在此处抱怨的行。如果不是,您可以编辑您的问题并将代码放在这里,这样我们就可以理解您在这些行中所做的事情。@drescherjm好的,我刚刚用一些更多的信息编辑了OP。@Jungle-如果您覆盖缓冲区的边界,您将得到C中所谓的“未定义行为”。由于行为未定义,您的代码可以正常工作,立即崩溃,稍后崩溃,或者导致恶魔从您的鼻子里飞出来。如果你的程序有未定义的行为,你不能对它进行推理,因为你不知道它会做什么。所以是的,这可能是你的问题。
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";
}
}
}
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 >>>
}