尝试使用宽字符串创建类时发生访问冲突

时间:2018-11-14 21:29:08

标签: c++ class crash access-violation widestring

我正在编写应读取调试日志,解析日志并激活Google文字转语音的代码,该代码将提供有关将其吐出的游戏的信息。

日志具有UTF-8编码,因此在我尝试创建类对象之前,即时消息在代码中使用宽字符串,然后编译并解析字符串。

然后我收到访问冲突:

'CMakeProject1.exe' (Win32): Loaded 'C:\Users\eirik\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.

Varibale Values right before crash

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

(已编辑)

1 个答案:

答案 0 :(得分:1)

代码存在的问题是std::wstring类中的Create_Object成员正在构造为值为0:

std::wstring Object_string = 0;

使用0构造std::basic_string(基于std::wstring)是不确定的行为。发生的是将0隐式转换为字符指针,因此将调用此构造函数:

std::wstring(const wchar_t* p)

请参见constructor (5) here for std::basic_string

此构造方法假定传入的指针不为null,并且指向以null终止的字符串。由于将访问空指针,因此会出现访问冲突错误。

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

std::wstring Object_string;

为进一步说明错误,这里是small example showing the issue。 删除初始化fixes the issue