Access violation reading location on class member

-1

So I'm currently developing a game engine, and I've had this Access violation reading location error for some time now. I tried to debug it a little and I know where it comes from but I can't figure out why it occurs.

So I have this Logger class : (Log.h)


class RED_API Logger
{
private:
    std::string name;

public:

    Logger(const std::string& name);
    ~Logger();

    template<typename... Args>
    void info(std::string fmt, Args... args)
    {
        time_t now = time(0);
        tm* ltm = localtime(&now);
        // white color
        std::cout << "\033[0;37m" << "[" << ltm->tm_hour << ":" << ltm->tm_min << ":" << ltm->tm_sec << 
        "] " << "[" << this->name << "] " << format(fmt, 0, args...) << "\n";
    }

    template<typename... Args>
    void success(std::string fmt, Args... args) { // similar content to info() }

    template<typename... Args>
    void warn(std::string fmt, Args... args) { // similar content to info() }

    template<typename... Args>
    void critical(std::string fmt, Args... args) { // similar content to info() }

Here is the implementation cpp file : (Log.cpp)

Logger::Logger(const std::string& _name)
    : name(_name)
{
    time_t now = time(0);
    tm* ltm = localtime(&now);
    std::cout << "[" << ltm->tm_hour << ":" << ltm->tm_min << ":" << ltm->tm_sec << "] " << "[" << 
    this->name << "] " << "Console Logger initialized." << std::endl;
}

Logger::~Logger()
{
    // similar to constructor
}

I use this class like so (still in Log.h) :

class RED_API Logging
{
private:
    static std::shared_ptr<Logger> console_logger;
    ...
public:
    static void init();
    static std::shared_ptr<Logger>& getConsoleLogger(); 
    ...

};

Log.cpp

std::shared_ptr<Logger> Logging::console_logger;

std::shared_ptr<Logger>& Logging::getConsoleLogger() { return console_logger; }

void Logging::init()
{
    console_logger = std::make_shared<Logger>("RED");       
}

With some macros (Log.h) :

#define RED_INFO(...)     ::Red::Logging::getConsoleLogger()->info(__VA_ARGS__)
#define RED_SUCCESS(...)  ::Red::Logging::getConsoleLogger()->success(__VA_ARGS__)
#define RED_WARN(...)     ::Red::Logging::getConsoleLogger()->warn(__VA_ARGS__)
#define RED_CRITICAL(...) ::Red::Logging::getConsoleLogger()->critical(__VA_ARGS__)

And finally here is where I use it, in my entry point (where the error occurs) :

int main(int argc, char** argv)
{
    Red::Logging::init();

    auto test = Red::Logging::getConsoleLogger(); // this is just a test to check to status of the shared_ptr

    RED_INFO("ENGINE STARTUP SEQUENCE."); // error here
    RED_SUCCESS("ENGINE STARTUP SEQUENCE TERMINATED SUCCESSFULLY.\n");

    ...
}

Here is the error i get in details :
"Exception thrown at 0x00007FFB2ECE1330 (vcruntime140d.dll) in RedHot_Keyboard.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF."

The debugger stops in a file called xstring at line 328:

static _Elem* copy(_Out_writes_(_Count) _Elem* const _First1, _In_reads_(_Count) const _Elem* const _First2,
    const size_t _Count) noexcept /* strengthened */ {
    // copy [_First2, _First2 + _Count) to [_First1, ...)
    return static_cast<_Elem*>(_CSTD memcpy(_First1, _First2, _Count)); // <- line 328
}

So here is what is happening :

The error occurs inside "RED_INFO(...)" in my entry point and more precisely "this->name" is causing the problem. When debugging, the shared_ptr on Logger gets initialized, inside init() the spy tells me the "name" member has value "RED" (which is cool) but as soon as I exit the init() the spy can't read the "name" member anymore and hence the read access violation when entering "RED_INFO(...)" (which calls "this->name").

This is where my understanding of the problem stops. It looks like the memory is freed but why would it be freed ? I don't understand.

Here is some more information about the problem:

  • It was working fine before and I haven't touched the Logging system
  • It works in Release mode
  • The engine is compiled as a DLL that is referenced in another project (the game) in my solution, so the issue might be related to that. I say that because I had issues with heap memory differences (between DLL and exe), that I fixed with the /MD, /MT, ... commands in visual studio.

I've given everything I know about the problem. Thanks in advance for your help !

EDIT:
I was asked to give more "debugging details" and to clarify the problem. My post is super long already and I've given everything I know so for short I have an Access violation reading location error that (a least for me) doesn't correspond to a coding error, all the details are above. And here is the full Visual studio solution : http://www.filedropper.com/redhotkeyboard

c++
asked on Stack Overflow Sep 13, 2020 by Linkapp • edited Sep 13, 2020 by Linkapp

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0