在类内部初始化unordered_map时出错

Errors with initialization of an unordered_map inside of a class

本文关键字:map 出错 unordered 内部 初始化      更新时间:2023-10-16

我得到的错误是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表示空指针。

检查你是否已经在你试图访问的变量中存储了一个值。

从错误对话框中实际显示的值可能会被偏移到的值,将关闭到上述位置,您只需跟踪整个程序。

您对错误的初始描述还指向至少试图取消对空指针的引用。