在类

时间:2015-10-19 02:39:49

标签: c++ simulation unordered-map

我得到的错误是Exception thrown at 0x00007FF77339C476 in VirusSimulator.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

我已经使用Visual Studio的调试器完成了我的代码,似乎错误来自尝试在类对象的无序映射上调用.size()。 具体来说,在“列表”的实现中:

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

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

1 个答案:

答案 0 :(得分:1)

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

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

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

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

更多代码会有所帮助。