RudjiGames / MTuner

MTuner is a C/C++ memory profiler and memory leak finder for Windows, PlayStation 4 and 3, Android and other platforms

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

debuggee crashes when calling some C++ standard library functions

milianw opened this issue · comments

I have a more complex application which is not running when profiling it with MTuner. I added logging there to find the culprit, and in the end it turns out the issue is a line that calls an innocent looking C++ standard library function.

I can reproduce this issue with this simple example:

#include <thread>
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>

int main()
{
    std::ofstream log("log.txt");
    if (!log.is_open())
        return 1;

    log << "init" << std::endl;

    using namespace std::chrono_literals;
    std::this_thread::sleep_for(100ms);

    log << "str" << std::endl;

    auto str = std::string("this is a very long string which should trigger some kind of allocation we see in mtuner then finally hopefully");
    auto str2 = str.substr();

    log << "out" << std::endl;

    std::cout << std::boolalpha << (str == str2) << std::endl;

    log << "fin" << std::endl;

    return 0;
}

When I compile this and run it in MTuner, then only the init line shows up in the log.txt file. When I run it outside of MTuner, then the app finishes until the fin line as expected.

That means the call to std::this_thread::sleep_for is triggering a crash in this case. In my more complex code base it was a std::string::substr call, but I couldn't reproduce this easily yet in a standalone example.

Could this maybe be a problem due to different runtimes? I.e. you are writing that MTuner is currently build with Visual Studio 2017, while I'm using 2022.

I would also be willing to debug this. For starters I tried to build MTuner myself, but I am stumbling over a few issues:

How does one install Lua for starters? You mention the lfs module, so do I need luarocks? I tried that, but I cannot compile the lfs module, I get a bunch of linker errors such as

C:\src>"\Program Files (x86)\LuaRocks\luarocks" install luafilesystem
Installing https://luarocks.org/luafilesystem-1.8.0-1.src.rock

luafilesystem 1.8.0-1 depends on lua >= 5.1 (5.1-1 provided by VM)
cl /nologo /MD /O2 -c -Fosrc/lfs.obj -IC:\Program Files (x86)\LuaRocks\include src/lfs.c
lfs.c
link -dll -def:lfs.def -out:lfs.dll C:\Program Files (x86)\LuaRocks/lua5.1.lib src/lfs.obj
Microsoft (R) Incremental Linker Version 14.33.31630.0
Copyright (C) Microsoft Corporation.  All rights reserved.

   Creating library lfs.lib and object lfs.exp
lfs.obj : error LNK2019: unresolved external symbol lua_gettop referenced in function file_utime
...

When I ignore that and continue with your MTuner install instructions, I get errors while building the solution in the moc step, so probably related to me failing to install the lfs module above...

Can you please document this a bit more on how you install lua and the lfs module on windows?

Many thanks

I now compiled the example code from the first comment with Visual Studio 2017, and it still quits early when run under MTuner. Any advise on how to debug this further would be appreciated. Can I maybe enable some kind of logging within MTuner to get an insight? Or can I somehow run the application and MTuner injection within a debugger environment, to debug this further? I have tried to poll for a file or sleep to allow me to runtime attach, but both exhibit C++ function calls that already lead to the early exit, so I'm at a loss on how to proceed.

I tried to debug this further with vsjitdebugger but the issue doesn't occur then. Indeed, when I run MTuner as administrator, the whole issue vanishes - so apparently it is some kind of esoteric windows ACL issue? Any idea on how to debug this, or do you generally recommend running MTuner as administrator?

This is a duplicate of #78 - same behavior, early process termination/crash.