1

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

(EDITED)

OptoCloud
  • 57
  • 9
  • Go through with a debugger and see where the error is occuring. – 001 Nov 14 '18 at 21:32
  • 1
    we need a [mcve] to help – Alan Birtles Nov 14 '18 at 21:34
  • Debugging has been done and i didnt get any useful info from it. – OptoCloud Nov 14 '18 at 21:36
  • `Create_Player` class has `playername` string but ctor uses `avatarname`. Is that right? Maybe try to eliminate as many global vars as you can. – 001 Nov 14 '18 at 21:40
  • *Debugging has been done and i didnt get any useful info from it.* then you're dong it wrong. Build a list of expectations. Step through the code line by line. When the program does something you do not expect, either the program is wrong or your expectations are. Either one needs to be corrected before you continue. – user4581301 Nov 14 '18 at 22:17
  • I simplified it :) Sorry, im used to debugging with Qt-creator, not visual studio. I will see what i can do. – OptoCloud Nov 14 '18 at 22:19
  • At a minimum, you should know which bit of code was the one that generated the violation. You could figure that out even without a debugger by adding logging. – David Schwartz Nov 14 '18 at 22:32
  • 1
    @OptoCloud `std::wstring Object_string = 0;` -- What is this supposed to do? A `std::wstring` cannot be 0 --you are not dealing with character pointers, so it makes no sense setting `std::wstring` to 0. That has all the earmarks of undefined behavior. Get rid of that `= 0;` and see if your problem magically disappears. – PaulMcKenzie Nov 14 '18 at 22:51
  • 1
    @OptoCloud [Please look here](http://coliru.stacked-crooked.com/a/04fa59f7f6128864). The issue is exactly what I pointed out. There is no such thing as a string being 0. Or at least, that is one of multiple issues -- but you see that the code is a [mcve], something you should have provided up front. – PaulMcKenzie Nov 14 '18 at 23:03
  • _im used to debugging with Qt-creator, not visual studio_ Visual Studio has, by far, the best debugger I have ever encountered. I can't recommend strongly enough that you learn how to use it if you plan to develop in Visual Studio. – Paul Sanders Nov 14 '18 at 23:24
  • Now it just seems to stop in front of: while (std::getline(file, line)) And it stays there – OptoCloud Nov 15 '18 at 00:02

1 Answers1

2

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.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45