Details
-
Bug
-
Resolution: Fixed
-
P1: Critical
-
None
-
5.12.1
-
None
Description
QDateTime::toString() crashes when using in a multithreaded application. Maybe it's a similar case like QTBUG-49473 (marked as "fixed in Qt 5.11.0").
How to reproduce (crashes immediately):
#include <QCoreApplication> #include <QThread> #include <QDateTime> class CustomThread : public QThread { Q_OBJECT protected: virtual void run(); }; void CustomThread::run() { QDateTime from = QDateTime::currentDateTime(); while (1) QString date = from.toString("yyyy-MM-dd hh:mm:ss"); } #include "main.moc" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); for (int var = 0; var < 1000; var++) { CustomThread *thread = new CustomThread(); thread->start(); } return a.exec(); }
gdb and valgrind detects a "double free" involving QLocal::system() and ~QSharedDataPointer:
==139554== Invalid free() / delete / delete[] / realloc() ==139554== at 0x4C2F5C2: operator delete(void*, unsigned long) (vg_replace_malloc.c:585) ==139554== by 0x4F47C1C: ~QSharedDataPointer (qshareddata.h:89) ==139554== by 0x4F47C1C: QLocale::~QLocale() (qlocale.cpp:938) ==139554== by 0x4F31B1B: QDateTime::toString(QStringView) const (qdatetime.cpp:3988) ==139554== by 0x4F31BF1: QDateTime::toString(QString const&) const (qdatetime.cpp:3994) ==139554== by 0x401277: CustomThread::run() (main.cc:16) ==139554== by 0x4EE8047: QThreadPrivate::start(void*) (qthread_unix.cpp:361) ==139554== by 0x5D6B6B9: start_thread (pthread_create.c:333) ==139554== by 0x5AA141C: clone (clone.S:109) ==139554== Address 0x89b3970 is 0 bytes inside a block of size 16 free'd ==139554== at 0x4C2F5C2: operator delete(void*, unsigned long) (vg_replace_malloc.c:585) ==139554== by 0x4F47C1C: ~QSharedDataPointer (qshareddata.h:89) ==139554== by 0x4F47C1C: QLocale::~QLocale() (qlocale.cpp:938) ==139554== by 0x4F31B1B: QDateTime::toString(QStringView) const (qdatetime.cpp:3988) ==139554== by 0x4F31BF1: QDateTime::toString(QString const&) const (qdatetime.cpp:3994) ==139554== by 0x401277: CustomThread::run() (main.cc:16) ==139554== by 0x4EE8047: QThreadPrivate::start(void*) (qthread_unix.cpp:361) ==139554== by 0x5D6B6B9: start_thread (pthread_create.c:333) ==139554== by 0x5AA141C: clone (clone.S:109) ==139554== Block was alloc'd at ==139554== at 0x4C2E476: operator new(unsigned long) (vg_replace_malloc.c:334) ==139554== by 0x4F54552: create (qlocale_p.h:337) ==139554== by 0x4F54552: clone (qlocale_p.h:392) ==139554== by 0x4F54552: QSharedDataPointer<QLocalePrivate>::detach_helper() (qshareddata.h:259) ==139554== by 0x4F4D5D9: detach (qshareddata.h:74) ==139554== by 0x4F4D5D9: data (qshareddata.h:81) ==139554== by 0x4F4D5D9: QLocale::system() (qlocale.cpp:2370) ==139554== by 0x4F31AFF: QDateTime::toString(QStringView) const (qdatetime.cpp:3988) ==139554== by 0x4F31BF1: QDateTime::toString(QString const&) const (qdatetime.cpp:3994) ==139554== by 0x401277: CustomThread::run() (main.cc:16) ==139554== by 0x4EE8047: QThreadPrivate::start(void*) (qthread_unix.cpp:361) ==139554== by 0x5D6B6B9: start_thread (pthread_create.c:333) ==139554== by 0x5AA141C: clone (clone.S:109)