Ensure the brushes are properly initialized in a thread-safe way
authorMilian Wolff <mail@milianw.de>
Mon, 13 Feb 2017 23:31:08 +0000 (00:31 +0100)
committerMilian Wolff <mail@milianw.de>
Mon, 13 Feb 2017 23:32:00 +0000 (00:32 +0100)
Fixes a crash under some circumstances, when multiple graphs
are build in parallel - something that needs to be investigated
separately.

src/analyze/gui/flamegraph.cpp

index 67b32cb..8f5f526 100644 (file)
@@ -218,12 +218,13 @@ QBrush brush()
     // intern the brushes, to reuse them across items which can be thousands
     // otherwise we'd end up with dozens of allocations and higher memory
     // consumption
-    static QVector<QBrush> brushes;
-    if (brushes.isEmpty()) {
+    static const QVector<QBrush> brushes = []() -> QVector<QBrush> {
+        QVector<QBrush> brushes;
         std::generate_n(std::back_inserter(brushes), 100, []() {
             return QColor(0, 190 + 50 * qreal(rand()) / RAND_MAX, 210 * qreal(rand()) / RAND_MAX, 125);
         });
-    }
+        return brushes;
+    }();
     return brushes.at(rand() % brushes.size());
 }