Access Violation when trying to create class with wide string


Im making a code which is supposed to read a debug log, parse the log, and activate a google text-to-speech which will give info about the game that spits it out.

The log has UTF-8 encoding, so im using wide strings in the code, the code compiles, and parses strings just fine until i try to create a class object.

Then i get a Access Violation:

'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.

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
    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 = passed_string;

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

int main()
    while (true)
        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////////////////////////////////
    return 1;


asked on Stack Overflow Nov 14, 2018 by OptoCloud • edited Aug 31, 2020 by OptoCloud

1 Answer


The issue with the code is that the std::wstring member is being constructed with the value of 0 here in your Create_Object class:

std::wstring Object_string = 0;

Constructing a std::basic_string (which std::wstring is based on) with 0 is undefined behavior. What occurs is that the 0 is implicitly converted to a character pointer, thus this constructor will be invoked:

std::wstring(const wchar_t* p)

See constructor (5) here for std::basic_string.

This constructor assumes the passed-in pointer is not null, and is pointing to a null-terminated string. Since a null pointer will be accessed, you get the access violation error.

The fix to this is just get rid of that initialization:

std::wstring Object_string;

To further demonstrate the error here is a small example showing the issue. Removing the initialization fixes the issue.

answered on Stack Overflow Nov 14, 2018 by PaulMcKenzie • edited Nov 14, 2018 by PaulMcKenzie

User contributions licensed under CC BY-SA 3.0