C++: Application crashes with abort() on logging function

  abort, c++, multithreading, mutex, windows

here is my code which is run on windows:


#include <iostream>
#include <sstream>
#include <fstream>
#include <shared_mutex>
#include <thread>
#include <iomanip>

class Log {
    static std::ofstream myfile;
    static std::shared_mutex m_smutex;

    static bool isLogEnabled();
    static void close() {
    template< typename... Args >
    static void debug(const char* format, Args... args) {
        std::unique_lock<std::shared_mutex> lock(Log::m_smutex);

        if (isLogEnabled()) {
            if (!Log::myfile.is_open()) {
                Log::myfile.open("C:tmplog.txt", std::ios_base::app);
        else {
            if (Log::myfile.is_open()) {

You can see I’ve deleted most of the code(…) because I narrowed down the problem.


#include <Windows.h>
#include "Log.h"

std::ofstream LogLine::myfile;
std::shared_mutex LogLine::m_smutex;
bool LogLine::isLogEnabled() {
    CHAR regeditPath[MAX_PATH] = { 0 };
    CHAR variable[] = "DEBUG_LOGS";
    GetEnvironmentVariableA(variable, regeditPath, MAX_PATH);

    return GetLastError() == ERROR_ENVVAR_NOT_FOUND ? false : true;

The process needs to be run always, that’s why I open and close the file everytime I log something. I want to be able to delete logs even when the process is still running. Maybe this is silly because there might be a flag which would allow me to delete the logs while file is open by some process.

Example usage:

Log::debug("little test %d", 10);

As you can see I use mutex because I want to call debug function from different threads. The problem is that after some time I get error code Abort() has been called but I can’t understand why.

Source: Windows Questions