1

我正在编写一个应该读取调试日志、解析日志并激活谷歌文本到语音的代码,这将提供有关吐出它的游戏的信息。

该日志具有 UTF-8 编码,因此我在代码中使用宽字符串,代码编译和解析字符串都很好,直到我尝试创建一个类对象。

然后我得到一个访问冲突:

'CMakeProject1.exe' (Win32): Loaded 'C:\Users\OptoCloud\CMakeBuilds\e36ef523-902d-0932-93cd-2201bbe1e731\build\x64-Release\CMakeProject1\CMakeProject1.exe'. Symbols loaded.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Program Files\AVAST Software\Avast\x64\aswhooka.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\ucrtbase.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\msvcp140.dll'. Cannot find or open the PDB file.
'CMakeProject1.exe' (Win32): Loaded 'C:\Windows\System32\vcruntime140.dll'. Cannot find or open the PDB file.
The thread 0x4174 has exited with code 0 (0x0).
Exception thrown at 0x00007FF7FBDA92B3 in CMakeProject1.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

崩溃前的变量值

#include <string>
#include <chrono>
#include <thread>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <cstdlib>
#include <vector>
#include <map>
#include <thread>
#include <mutex>
#include <locale>
#include <codecvt>

std::wstring line;
std::wstring passed_string;
std::string path = "c:\\Users\\OptoCloud\\AppData\\LocalLow\\Game\\output_log.txt";

std::wifstream file(path, std::ios::binary);

class Create_Object
{
public:
    Create_Object();
    Create_Object(std::wstring passed_string);
    std::wstring Object_string = 0;
};
Create_Object::Create_Object() {}
Create_Object::Create_Object(std::wstring passed_string)
{
    Object_string.reserve(passed_string.length());
    Object_string = passed_string;
}

std::map<std::wstring, Create_Object> objectlist;

int main()
{
    while (true)
    {
        file.open(path.data());
        if (file.is_open())
        {
            while (std::getline(file, line))
            {
                if (line.find(L"DELIMITER1") != std::string::npos)
                {
                    passed_string = line.substr(line.find(L"DELIMITER1"), line.find(L"DELIMITER2"));
                    objectlist[passed_string] = Create_Object(passed_string); ///////////////////////POINT OF CRASH////////////////////////////////
                }
                file.close();
                break;
            }
        }
    }
    return 1;
}

(已编辑)

4

1 回答 1

2

代码的问题是在您的类std::wstring中使用 0 的值构造成员:Create_Object

std::wstring Object_string = 0;

用 0构造一个std::basic_stringstd::wstring基于)是未定义的行为。发生的情况是 0 被隐式转换为字符指针,因此将调用此构造函数:

std::wstring(const wchar_t* p)

有关std::basic_string ,请参见此处的构造函数 (5) 。

此构造函数假定传入的指针不为 null,并且指向一个以 null 结尾的字符串。由于将访问空指针,因此您会收到访问冲突错误。

解决此问题的方法是摆脱该初始化:

std::wstring Object_string;

为了进一步演示错误,这里是一个显示问题的小示例。删除初始化解决了这个问题

于 2018-11-14T23:35:49.037 回答