Hi,
I'm using a simple class called Scope that prints the current function name in cosructor and destructor for debugging purposes.
So I get a message when the programs executes a function and when it leaves.
This is the header
#ifndef SCOPEDEBUG_H
#define SCOPEDEBUG_H
#define SHELL_RESET "\033[0m"
#define SHELL_RED "\033[031m"
#define SHELL_GREEN "\033[032m"
#define SHELL_YELLOW "\033[033m"
#define SHELL_BLUE "\033[034m"
#include <QDebug>
#include <QString>
#include <QElapsedTimer>
//#define NO_DEBUG_CALL_TRACE
#ifndef NO_DEBUG_CALL_TRACE
class Scope
{
public:
Scope(const char *fn, const char *s="", const char* e="");
~Scope();
const char *func, *start, *end;
};
class ScopeTimer : Scope
{
public:
ScopeTimer(const char *fn, const char *s="", const char* e="");
~ScopeTimer();
QElapsedTimer timer;
};
# define DEBUG_ENTRY_NAME(name) Scope DBG(name)
# define DEBUG_ENTRY DEBUG_ENTRY_NAME(__PRETTY_FUNCTION__)
# define DEBUG_COLOR_ENTRY(color) Scope DBG(__PRETTY_FUNCTION__, color, SHELL_RESET)
# define DEBUG_IMPORTANT_ENTRY DEBUG_COLOR_ENTRY(SHELL_GREEN)
# define DEBUG_TIME_ENTRY ScopeTimer DBG(__PRETTY_FUNCTION__)
#else
# define DEBUG_ENTRY_NAME(name)
# define DEBUG_ENTRY
# define DEBUG_COLOR_ENTRY(color)
# define DEBUG_IMPORTANT_ENTRY
# define DEBUG_TIME_ENTRY
#endif // NO_DEBUG_CALLTRACE
#endif // SCOPEDEBUG_H
#ifndef SCOPEDEBUG_H
#define SCOPEDEBUG_H
#define SHELL_RESET "\033[0m"
#define SHELL_RED "\033[031m"
#define SHELL_GREEN "\033[032m"
#define SHELL_YELLOW "\033[033m"
#define SHELL_BLUE "\033[034m"
#include <QDebug>
#include <QString>
#include <QElapsedTimer>
//#define NO_DEBUG_CALL_TRACE
#ifndef NO_DEBUG_CALL_TRACE
class Scope
{
public:
Scope(const char *fn, const char *s="", const char* e="");
~Scope();
const char *func, *start, *end;
};
class ScopeTimer : Scope
{
public:
ScopeTimer(const char *fn, const char *s="", const char* e="");
~ScopeTimer();
QElapsedTimer timer;
};
# define DEBUG_ENTRY_NAME(name) Scope DBG(name)
# define DEBUG_ENTRY DEBUG_ENTRY_NAME(__PRETTY_FUNCTION__)
# define DEBUG_COLOR_ENTRY(color) Scope DBG(__PRETTY_FUNCTION__, color, SHELL_RESET)
# define DEBUG_IMPORTANT_ENTRY DEBUG_COLOR_ENTRY(SHELL_GREEN)
# define DEBUG_TIME_ENTRY ScopeTimer DBG(__PRETTY_FUNCTION__)
#else
# define DEBUG_ENTRY_NAME(name)
# define DEBUG_ENTRY
# define DEBUG_COLOR_ENTRY(color)
# define DEBUG_IMPORTANT_ENTRY
# define DEBUG_TIME_ENTRY
#endif // NO_DEBUG_CALLTRACE
#endif // SCOPEDEBUG_H
To copy to clipboard, switch view to plain text mode
And this is the cpp file:
#include "app/scopedebug.h"
#ifndef NO_DEBUG_CALL_TRACE
static uint thread_local stackLevel = 0;
Scope::Scope(const char *fn, const char *s, const char *e) :
func(fn),
start(s),
end(e)
{
qDebug().nospace().noquote()
<< start <<
QByteArray(" ").
repeated(stackLevel
) <<
">>> " << func << end;
stackLevel++;
}
Scope::~Scope()
{
stackLevel--;
qDebug().nospace().noquote()
<< start <<
QByteArray(" ").
repeated(stackLevel
) <<
"<<< " << func << end;
}
ScopeTimer::ScopeTimer(const char *fn, const char *s, const char *e) :Scope(fn, s, e)
{
timer.start();
}
ScopeTimer::~ScopeTimer()
{
qDebug() << "TOOK" << timer.elapsed() << "ms";
}
#endif
#include "app/scopedebug.h"
#ifndef NO_DEBUG_CALL_TRACE
static uint thread_local stackLevel = 0;
Scope::Scope(const char *fn, const char *s, const char *e) :
func(fn),
start(s),
end(e)
{
qDebug().nospace().noquote()
<< start << QByteArray(" ").repeated(stackLevel) << ">>> " << func << end;
stackLevel++;
}
Scope::~Scope()
{
stackLevel--;
qDebug().nospace().noquote()
<< start << QByteArray(" ").repeated(stackLevel) << "<<< " << func << end;
}
ScopeTimer::ScopeTimer(const char *fn, const char *s, const char *e) :Scope(fn, s, e)
{
timer.start();
}
ScopeTimer::~ScopeTimer()
{
qDebug() << "TOOK" << timer.elapsed() << "ms";
}
#endif
To copy to clipboard, switch view to plain text mode
The crash happens doesn't always happen and also it only happens if I start my program with debugger.
Also if it's relevant I also start a QRunnable in the global threadpool and it also calls qDebug() at about the same time I think but the documentation says that qDebug() is thread-safe.
Qt Version: 5.11.3 kit
Compiler: MinGW 32 bit
OS: Windows 8.1 64 bit
This is the trace:
1 QMessageLogger::fatal qlogging.cpp 872 0x6b98fc96
2 qt_assert qglobal.cpp 3202 0x6b98a961
3 QRingBuffer::free qringbuffer.cpp 113 0x6b9e7295
4 QIODevicePrivate::QRingBufferRef::free qiodevice_p.h 100 0x6ba594f0
5 QFileDevice::flush qfiledevice.cpp 294 0x6ba594f0
6 QTextStreamPrivate::flushWriteBuffer qtextstream.cpp 609 0x6bb63e89
8 myMessageOutput main.cpp 48 0x406bbc
9 qt_message_print qlogging.cpp 1791 0x6b98f5dd
10 qt_message_output qlogging.cpp 1849 0x6b99041d
11 QDebug::~QDebug qdebug.cpp 156 0x6ba4d2bf
12 Scope::Scope scopedebug.cpp 12 0x40cf5a
13 JobPathEditor::clearJob jobpatheditor.cpp 293 0x41240f
1 QMessageLogger::fatal qlogging.cpp 872 0x6b98fc96
2 qt_assert qglobal.cpp 3202 0x6b98a961
3 QRingBuffer::free qringbuffer.cpp 113 0x6b9e7295
4 QIODevicePrivate::QRingBufferRef::free qiodevice_p.h 100 0x6ba594f0
5 QFileDevice::flush qfiledevice.cpp 294 0x6ba594f0
6 QTextStreamPrivate::flushWriteBuffer qtextstream.cpp 609 0x6bb63e89
7 QTextStream::~QTextStream qtextstream.cpp 1163 0x6bb63f57
8 myMessageOutput main.cpp 48 0x406bbc
9 qt_message_print qlogging.cpp 1791 0x6b98f5dd
10 qt_message_output qlogging.cpp 1849 0x6b99041d
11 QDebug::~QDebug qdebug.cpp 156 0x6ba4d2bf
12 Scope::Scope scopedebug.cpp 12 0x40cf5a
13 JobPathEditor::clearJob jobpatheditor.cpp 293 0x41240f
To copy to clipboard, switch view to plain text mode
QRingBuffer::free has an assert and I get always 70 bytes < 45 -> assert failure.
Bookmarks