From 69d770e90811957066432bd7ff306c318ae84d0f Mon Sep 17 00:00:00 2001
From: adazem009 <68537469+adazem009@users.noreply.github.com>
Date: Mon, 18 Sep 2023 16:40:10 +0200
Subject: [PATCH 1/2] fix #186: Keep track of whether the project is running

---
 src/engine/internal/engine.cpp | 9 ++++++++-
 src/engine/internal/engine.h   | 1 +
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/engine/internal/engine.cpp b/src/engine/internal/engine.cpp
index 135d928d..7176d70d 100644
--- a/src/engine/internal/engine.cpp
+++ b/src/engine/internal/engine.cpp
@@ -132,7 +132,7 @@ void Engine::frame()
 
         do {
             script->run();
-            if (script->atEnd()) {
+            if (script->atEnd() && m_running) {
                 for (auto &[key, value] : m_runningBroadcastMap) {
                     size_t index = 0;
 
@@ -152,6 +152,8 @@ void Engine::frame()
         } while (!script->atEnd() && !m_breakFrame);
     }
 
+    assert(m_running || m_scriptsToRemove.empty());
+
     for (auto script : m_scriptsToRemove) {
         size_t index = -1;
         for (size_t i = 0; i < m_runningScripts.size(); i++) {
@@ -169,6 +171,7 @@ void Engine::frame()
 void Engine::start()
 {
     m_timer->reset();
+    m_running = true;
 
     for (auto target : m_targets) {
         auto gfBlocks = target->greenFlagBlocks();
@@ -180,6 +183,8 @@ void Engine::start()
 void Engine::stop()
 {
     m_runningScripts.clear();
+    m_scriptsToRemove.clear();
+    m_running = false;
 }
 
 void Engine::startScript(std::shared_ptr<Block> topLevelBlock, std::shared_ptr<Target> target)
@@ -344,6 +349,8 @@ void Engine::run()
 
         lastFrameTime = currentTime;
     }
+
+    stop();
 }
 
 bool Engine::broadcastRunning(unsigned int index, VirtualMachine *sourceScript)
diff --git a/src/engine/internal/engine.h b/src/engine/internal/engine.h
index 04f519ba..eb9f5c89 100644
--- a/src/engine/internal/engine.h
+++ b/src/engine/internal/engine.h
@@ -108,6 +108,7 @@ class Engine : public IEngine
         std::unique_ptr<ITimer> m_defaultTimer;
         ITimer *m_timer = nullptr;
 
+        bool m_running = false;
         bool m_breakFrame = false;
         bool m_skipFrame = false;
         bool m_lockFrame = false;

From 65463e459ffa1fe349b470e5ef0d245cbe2085b1 Mon Sep 17 00:00:00 2001
From: adazem009 <68537469+adazem009@users.noreply.github.com>
Date: Mon, 18 Sep 2023 16:40:34 +0200
Subject: [PATCH 2/2] Add test for #186

---
 test/engine/engine_test.cpp                    |   8 ++++++++
 test/regtest_projects/186_crash_after_stop.sb3 | Bin 0 -> 1025 bytes
 2 files changed, 8 insertions(+)
 create mode 100644 test/regtest_projects/186_crash_after_stop.sb3

diff --git a/test/engine/engine_test.cpp b/test/engine/engine_test.cpp
index 12620f4a..443d29f4 100644
--- a/test/engine/engine_test.cpp
+++ b/test/engine/engine_test.cpp
@@ -398,3 +398,11 @@ TEST(EngineTest, Clones)
             ASSERT_EQ((*list)[i].toString(), "1 2"); // TODO: Change this to "12" after #188 is fixed
     }
 }
+
+TEST(EngineTest, NoCrashAfterStop)
+{
+    // Regtest for #186
+    Project p("regtest_projects/186_crash_after_stop.sb3");
+    ASSERT_TRUE(p.load());
+    p.run();
+}
diff --git a/test/regtest_projects/186_crash_after_stop.sb3 b/test/regtest_projects/186_crash_after_stop.sb3
new file mode 100644
index 0000000000000000000000000000000000000000..5ca02a980edd6bbef4865a3c6b5b4cfcb40152c9
GIT binary patch
