UI: MemPlumber: Print stack traces

This commit is contained in:
Nodir Temirkhodjaev 2024-07-20 19:38:45 +05:00
parent 4c6ea29491
commit d03ec2027e
2 changed files with 21 additions and 12 deletions

View File

@ -63,6 +63,10 @@ class MemPlumberInternal {
#else
m_ProgramStarted = -1;
#endif //COLLECT_STATIC_VAR_DATA
#ifdef USE_CPPTRACE
cpptrace::absorb_trace_exceptions(false);
#endif
}
FILE* openFile(const char* fileName, bool append) {
@ -105,6 +109,7 @@ class MemPlumberInternal {
public:
bool isStarted() const { return m_Started; }
FILE* dumper() const { return m_Dumper; }
static MemPlumberInternal& getInstance() {
static MemPlumberInternal instance;
@ -362,15 +367,18 @@ class MemPlumberInternal {
// We can use dbghelp but it's not supported on MinGW. Need to figure out a way to solve it on all platforms
const char* getCaller() {
if (!MemPlumberInternal::getInstance().isStarted())
return "";
#ifdef USE_CPPTRACE
const auto objTrace = cpptrace::generate_object_trace(/*skip=*/3, /*max_depth=*/1);
const auto trace = objTrace.resolve();
const auto line = trace.to_string();
if (!line.empty()) {
return line.c_str();
static bool isDumping = false;
auto dumper = MemPlumberInternal::getInstance().dumper();
if (dumper && !isDumping) {
isDumping = true;
const auto objTrace = cpptrace::generate_object_trace(/*skip=*/1, /*max_depth=*/9);
const auto trace = objTrace.resolve();
fprintf(dumper, "Trace: %s\n", trace.to_string().c_str());
isDumping = false;
}
#endif

View File

@ -15,7 +15,7 @@ const char *const LOG_FILE_PATH = "memplumber.txt";
void MemPlumberUtil::start()
{
MemPlumber::start();
MemPlumber::start(/*verbose=*/true, LOG_FILE_PATH, /*append=*/true);
}
void MemPlumberUtil::stop()
@ -25,12 +25,13 @@ void MemPlumberUtil::stop()
void MemPlumberUtil::checkAndStop()
{
stop();
QTimer::singleShot(10, [&] {
size_t memLeakCount;
uint64_t memLeakSize;
MemPlumber::memLeakCheck(memLeakCount, memLeakSize, /*verbose=*/true, LOG_FILE_PATH);
stop();
MemPlumber::memLeakCheck(
memLeakCount, memLeakSize, /*verbose=*/true, LOG_FILE_PATH, /*append=*/true);
qCInfo(LC) << "Number of leaked objects:" << memLeakCount
<< "Total amount of memory leaked:" << memLeakSize;