From f1edf7d8209e10aea0378412f6077e2c366ed07d Mon Sep 17 00:00:00 2001
From: Elliu <elliu@hashi.re>
Date: Thu, 21 Jul 2022 21:53:02 +0200
Subject: [PATCH] Prevent crashes due to badly initialized of current Document

currentVivyDocument and currentScriptDocument macros don't always return
the "parent" document. This assumption lead to some badly initialized
data that considered the wrong document to be the "parent" of the view.

Fix the bad usage of those macros
---
 src/Lib/Ass/Line.cc                           |  1 +
 .../AudioVisualizer/TimingScene.cc            | 31 ++++++++++++-------
 src/VivyApplication.hh                        |  6 ++++
 3 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/src/Lib/Ass/Line.cc b/src/Lib/Ass/Line.cc
index 1bebb0bc..756f6c47 100644
--- a/src/Lib/Ass/Line.cc
+++ b/src/Lib/Ass/Line.cc
@@ -7,6 +7,7 @@
 using namespace Vivy::Ass;
 
 Line::Line()
+    // FIXME SEVERE: use of currentVivyDocument?
     : assFactory(currentVivyDocument->getAssSubDocument()->getFactory())
 {
 }
diff --git a/src/UI/DocumentViews/AudioVisualizer/TimingScene.cc b/src/UI/DocumentViews/AudioVisualizer/TimingScene.cc
index 3aee8bbc..dcca1fb1 100644
--- a/src/UI/DocumentViews/AudioVisualizer/TimingScene.cc
+++ b/src/UI/DocumentViews/AudioVisualizer/TimingScene.cc
@@ -81,18 +81,25 @@ TimingScene::rebuildScene()
     addItem(cursor);
     cursor->setPos(0, TimingUtils::axisHeight());
 
-    if (auto assDocument = currentVivyDocument->getAssSubDocument()) {
-        QVector<Ass::LinePtr> lines = assDocument->getLines();
-        for (int i = 0; i < lines.size(); ++i) {
-            if (auto line = lines.at(i).get()) {
-                TimingLine *l = new TimingLine(line, i);
-                l->setVisible(false);
-                addItem(l);
-                timingLines.append(l);
-                if (auto p = rootView.lock()) {
-                    if (auto assLinesModel = p->getAssLinesModel()) {
-                        connect(l, &TimingLine::lineChanged, assLinesModel,
-                                &AssLinesModel::updateLine);
+    // FIXME: bruh
+    if (auto p = rootView.lock()) {
+        if (auto v = p.get()) {
+            if (auto d = v->getDocument()) {
+                if (auto assDocument = d->getAssSubDocument()) {
+                    QVector<Ass::LinePtr> lines = assDocument->getLines();
+                    for (int i = 0; i < lines.size(); ++i) {
+                        if (auto line = lines.at(i).get()) {
+                            TimingLine *l = new TimingLine(line, i);
+                            l->setVisible(false);
+                            addItem(l);
+                            timingLines.append(l);
+                            if (auto p = rootView.lock()) {
+                                if (auto assLinesModel = p->getAssLinesModel()) {
+                                    connect(l, &TimingLine::lineChanged, assLinesModel,
+                                            &AssLinesModel::updateLine);
+                                }
+                            }
+                        }
                     }
                 }
             }
diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh
index c802a011..e516b3e0 100644
--- a/src/VivyApplication.hh
+++ b/src/VivyApplication.hh
@@ -16,6 +16,12 @@
 #define VIVY_APP_LOGGABLE_OBJECT_BY_STORED_NAME(name, logger) \
     VIVY_LOGGABLE_OBJECT_BY_STORED_NAME(vivyApp->getLogSink(), name, logger)
 
+/*
+ * Be careful when using those two macros
+ * What you get here is the document currently selected as the current tab
+ * Fore exemple, there is no guarantee that this is the same document that
+ * the parent of a subDocument View you're calling those macros from
+ */
 #define currentVivyDocument   dynamic_cast<VivyDocument *>(vivyApp->getCurrentDocument())
 #define currentScriptDocument dynamic_cast<ScriptDocument *>(vivyApp->getCurrentDocument())
 
-- 
GitLab