Unsolved Crash with QFontMetrics::elidedText
-
Calling QFontMetrics::elidedText leads to a crash (call on GUI thread), which crash coming from QFontPrivateData.
Here is the callstack:Qt5Guid.dll!QAtomicOpsBySize<4>::ref<int>(int & _q_value) Line 308 C++ Qt5Guid.dll!QBasicAtomicInteger<int>::ref() Line 113 C++ Qt5Guid.dll!QExplicitlySharedDataPointer<QFontPrivate>::QExplicitlySharedDataPointer<QFontPrivate>(QFontPrivate * adata) Line 264 C++ Qt5Guid.dll!QFont::QFont(QFontPrivate * data) Line 565 C++ Qt5Guid.dll!QFontMetrics::elidedText(const QString & text, Qt::TextElideMode mode, int width, int flags) Line 905 C++
This is on Windows 10, Qt 5.9.1 with MSVC 2015 (No optimization flags enabled)
-
Please provide a minimal, compilable example.
-
Hi,
Please provide a minimal compilable example that triggers the crash.
You should also consider updating your version of Qt. Qt 5.9.1 is quite old.
-
static const QFont font{ "Consolas", 9, 100 }; // This function is frequently called, font and width remain constant std::pair<int, const QFontMetrics&> GetInfo() { static QFontMetrics metrics{font}; static int width = metrics.averageCharWidth() * MAX_CHARS_REQUIRED; // some constant return std::make_pair(width, metrics); } int main(int argc, char** argv) { QApplication app(argc, argv); auto pair = GetInfo(); auto txt = pair.second.elidedText("some long text", Qt::ElideRight, pair.first); // crash here std::cout << text.toStdString(); return app.exec(); }
-
@SGaist Unfortunately, I can't update the Qt version. We might eventually mode to Qt6 (at a later date)
-
That code has an issue, it's missing a QApplication object. It's mandatory to setup Qt's internals that needs to access graphic system related data/information.
-
@SGaist I ommitted certain pieces. QApplication as well as QWindow exists in the real one.
-
Here is the issue: with missing bits and pieces, your minimal application does not reflect your situation so it is likely hiding the real issue.
-
I can't really copy paste the entire application code here. But what are the possible scenarios in which I can face error from QFontPrivate ? Or it too vague to guess?
-
You return a dangling QFontMetrics reference.
-
To be more precise: you're failing to due std::make_pair: "The deduced types V1 and V2 are std::decay<T1>::type and std::decay<T2>::type"
So either use
return std::pair<int, const QFontMetrics &>(width, metrics);
or since you prefer (although very very inconsistent) brace initializationreturn {width, metrics};