在类内部初始化unordered_map时出错
Errors with initialization of an unordered_map inside of a class
我得到的错误是Exception thrown at 0x00007FF77339C476 in VirusSimulator.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
我已经用Visual Studio的调试器检查了我的代码,似乎错误来自尝试在无序的类对象映射上调用.size()。具体来说,在"list"的实现中:
size_type size() const _NOEXCEPT
{ // return length of sequence
return (this->_Mysize());
}
当调试器逐步调试并注意局部变量时,我看到:this 0xccccccccccccce0c folders={ size=??? }
(文件夹是类变量的映射)下面是int main()的一部分,我在每台计算机的驱动器的文件夹映射中手动初始化一个默认文件夹:
vector<Computer> macs;
for (int i = 0; i < mac_amount; i++)
{
macs.push_back(Computer(OSX)); //Initialize a computer with an operating system
}
for (int i = 0; i < macs.size(); i++)
{
//... Here is some code initializing connections between the computer objects
//and here is the manual insert of folders into the map
macs[i].getDrive()->addFolder("default"); //This used to be in the computers constructor but I moved it out here for testing
}
addFolder定义:
void Harddrive::addFolder(string name)
{
Folder new_folder;
new_folder.set_name(name);
folders.insert(std::pair<string, Folder>(name, new_folder));
}
基本上,一个随机的计算机对象然后运行一个病毒,试图通过访问它拥有的连接列表来将自己安装到所有其他连接的计算机对象上,该列表包含指向其他计算机对象的指针。然后它取消引用这些,并尝试在每个计算机的硬盘驱动器上找到默认文件夹,但随后失败,声称文件夹未初始化?
如果需要我的代码的任何其他部分,那么完整的代码可以在https://github.com/BananaSky/VirusSimulator/tree/UnstableNetworkAdditions找到我已经测试了大部分代码的bug,这就是为什么我只发布了这么一小部分。
任何帮助都是非常感激的!(但是,这里已经很晚了,我可能很快就要睡觉了,所以如果我不能马上回复,我很抱歉)
--ALSO: Please note that this is just a simulation and that I'm not actually intending to create any form of computer virus.
以下是发生错误的代码段(在Virus.cpp中):
vector<int> vulnerableConnectionIPs = installedOn->getNetworkAdapter()->getConnection()->getConnections();
for (int i = 0; i < vulnerableConnectionIPs.size(); i++)
{
Computer* accessRequest = installedOn->getNetworkAdapter()->getConnection()->getConnect(vulnerableConnectionIPs[i])->giveAccess();
if (accessRequest != NULL && accessRequest != installedOn)
{
if (accessRequest->getDrive()->getFolders()) //Error occurs here
{
accessRequest->Install(this, "default"); //Infect if infectable :)
}
else
{
cout << "No folders exist on " << accessRequest->getDrive()->getModel() << endl;
}
}
}
我正在小规模复制这个,我可能会在明天发布
0xcccccccccccccccc
(64位)或0xcccccccc
(32位)的内存地址是Visual Studio表示uninitialized block of memory
的方式。还有0xfeeefeee
表示已经空闲,0x00000000
表示空指针。
检查你是否已经在你试图访问的变量中存储了一个值。
从错误对话框中实际显示的值可能会被偏移到的值,将关闭到上述位置,您只需跟踪整个程序。
您对错误的初始描述还指向至少试图取消对空指针的引用。
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 使用一个考虑到std::map中键值的滚动或换行的键
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 尝试对 std::map<int, class> 进行排序时出错
- 传递非自动常量左值引用时,for_each、map和lambda出错
- 从initializer_list构造 std::map<T、<S>unique_ptr> 时出错
- 打印 Map 的元素值时出错
- stl:map插入值时出错
- 在c++中将map传递给模板函数时出错
- 在XCode/Clang中使用std::shared_ptr实例化std::map出错
- 试图从std::map中查找const char*键时出错
- 传递std::map作为模板模板参数时出错
- 尝试将'=='重载传递给 std::map 比较器时出错