【问题标题】:Errors with initialization of an unordered_map inside of a class类内部 unordered_map 的初始化错误
【发布时间】:2015-10-19 02:39:48
【问题描述】:

我得到的错误是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 找到完整代码 大部分代码我已经测试了错误,这就是为什么我只发布了这么小的一部分。

非常感谢任何帮助! (不过,这里时间不早了,我可能很快就要睡觉了,所以如果我不能马上回复,我很抱歉)

--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;
            }
        }
    }

我正在努力以较小的规模复制它,我可能会在明天之前发布它

【问题讨论】:

  • 我已尝试复制此内容,但无法重现相同的错误。我会看看我能做什么
  • 我扫描了你的代码。看起来您正在创建一个库。如果没有驱动程序,几乎不可能分辨出执行顺序是什么,从而查看有问题的代码是什么。

标签: c++ simulation unordered-map


【解决方案1】:

0xcccccccccccccccc(64 位)或0xcccccccc(32 位)的内存地址是 Visual Studio 表示uninitialized block of memory 的方式。还有 0xfeeefeee 表示已经释放的指针,0x00000000 表示空指针。

检查您是否在尝试访问的变量中实际存储了一个值。

错误对话框中实际显示的值可能与上述位置接近的值有偏差,您只需通过程序进行跟踪即可。

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

更多代码会有所帮助。

【讨论】:

  • 谢谢。当我在调试器中查看未初始化的内容时,它真的帮助了我。
猜你喜欢
  • 2020-12-03
  • 1970-01-01
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
相关资源
最近更新 更多