Unhandled exception at 0x00007FFE1B12E261 (KernelBase.dll): 0xC0000005: Access violation writing location 0x00007FF7837A8D3E. occurred

-1

I have a problem when I run a program I built. It is a chess interface and I am trying to integrate the Stockfish chess engine with the interface. When I debug, I get this error message:

Unhandled exception at 0x00007FFE1B12E261 (KernelBase.dll): 0xC0000005: Access violation writing location 0x00007FF7837A8D3E.

If I run the program normally, the program will run for a few seconds and then crash. I'm using SFML to display the board and pieces. I know the problem is with my header file(used to connect the engine and GUI) because if I remove the utilization of the function that is defined in the header file from my main.cpp file, no errors show up and everything displays correctly. I think the problem is with the CreateProcess() function. I also think that the problem may lie in the fact that I cast the path variable to an LPWSTR in the header file but cast the string "stockfish.exe" to a char* to resolve some errors, but I am not sure. stockfish.exe is the name of the executable for the engine.

Here's my code:

#ifndef CONNECTOR_H
#define CONNECTOR_H

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>

STARTUPINFO sti = { 0 };
SECURITY_ATTRIBUTES sats = { 0 };
PROCESS_INFORMATION pi = { 0 };
HANDLE pipin_w, pipin_r, pipout_w, pipout_r;
BYTE buffer[2048];
DWORD writ, excode, read, available;


void ConnectToEngine(char* path)
{
    pipin_w = pipin_r = pipout_w = pipout_r = NULL;
    sats.nLength = sizeof(sats);
    sats.bInheritHandle = TRUE;
    sats.lpSecurityDescriptor = NULL;

    CreatePipe(&pipout_r, &pipout_w, &sats, 0);
    CreatePipe(&pipin_r, &pipin_w, &sats, 0);

    sti.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
    sti.wShowWindow = SW_HIDE;
    sti.hStdInput = pipin_r;
    sti.hStdOutput = pipout_w;
    sti.hStdError = pipout_w;

    CreateProcessW(NULL, (LPWSTR)path, NULL, NULL, TRUE, 0, NULL, NULL, &sti, &pi);
}


std::string getNextMove(std::string position)
{
    std::string str;
    position = "position startpos moves " + position + "\ngo\n";

    WriteFile(pipin_w, position.c_str(), position.length(), &writ, NULL);
    Sleep(500);

    PeekNamedPipe(pipout_r, buffer, sizeof(buffer), &read, &available, NULL);
    do
    {
        ZeroMemory(buffer, sizeof(buffer));
        if (!ReadFile(pipout_r, buffer, sizeof(buffer), &read, NULL) || !read) break;
        buffer[2048] = 0;
        str += (char*)buffer;
    } while (read >= sizeof(buffer));

    int n = str.find("bestmove");
    if (n != -1) return str.substr(int(n + 9), 4);

    return "error";
}


void CloseConnection()
{
    WriteFile(pipin_w, "quit\n", 5, &writ, NULL);
    if (pipin_w != NULL) CloseHandle(pipin_w);
    if (pipin_r != NULL) CloseHandle(pipin_r);
    if (pipout_w != NULL) CloseHandle(pipout_w);
    if (pipout_r != NULL) CloseHandle(pipout_r);
    if (pi.hProcess != NULL) CloseHandle(pi.hProcess);
    if (pi.hThread != NULL) CloseHandle(pi.hThread);
}


#endif CONNECTOR_H

I'm using Visual Studio if that helps.

c++
sfml
chess
asked on Stack Overflow Dec 16, 2019 by xXH4CKST3RXx

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0