在C++向量中push_back() 时访问违规写入位置
Access violation writing location when push_back() in a C++ vector?
>背景信息
当抛出"访问冲突写入位置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。这修复了堆上发生的所有损坏。感谢那些帮助过的人。
故事的寓意:始终在堆错误损坏您的构建之前修复它们损坏!
相关文章:
- 写入位置0x0000000C时发生访问冲突
- 使用迭代器时如何访问对象在向量中的位置?
- 0xC0000005:访问冲突写入位置0xCDCDCDCD动态分配错误
- Cuda:访问违规写入位置0x0000000000000000
- 0xC0000005:访问冲突读取位置 0x00000000. 重载 == 运算符的问题
- 映射唯一值和重复值的有效方法.可以访问键或值的位置
- 访问特定阵列位置/索引时出现分段错误
- 创建一个类来访问和指定向量类型,并构建一个获取位置并为其分配区域的类
- (此函数用于计算 SMA).在 FA.exe 中0x00509159引发异常: 0xC0000005:访问冲突读取位置0
- 0xC0000005:读取位置0xCDCDCDC1时发生访问冲突
- 0xC0000005:读取位置0xCDCDCDCD时发生访问冲突
- 读取类的析构函数中的位置时发生访问冲突
- 在尝试使用CUDA分配内存时,我遇到了访问冲突写入位置错误
- 快速访问 ifs() C++中的文件位置
- 创建 2D 数组0xCCCCCCCC访问冲突写入位置
- 0x0F50DF58:0xC0000005:访问冲突读取位置0x0047CA04时未处理的异常
- 在 DLL 中嵌入 Python:Py_DECREF列表对象时访问冲突读取位置
- 处于默认参数位置的 Lambda 无法访问好友成员。这是编译器错误吗?
- 在 C 代码中调用 Python 函数时第三次出现访问冲突写入位置错误
- 确定打开进程的内存访问位置