Skip to content

Commit d860f78

Browse files
authored
Merge pull request #209 from scratchcpp/fix_crash_after_stopping
Fix #186: Fix crash after stopping the project
2 parents 6ae3b33 + 65463e4 commit d860f78

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

src/engine/internal/engine.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void Engine::frame()
132132

133133
do {
134134
script->run();
135-
if (script->atEnd()) {
135+
if (script->atEnd() && m_running) {
136136
for (auto &[key, value] : m_runningBroadcastMap) {
137137
size_t index = 0;
138138

@@ -152,6 +152,8 @@ void Engine::frame()
152152
} while (!script->atEnd() && !m_breakFrame);
153153
}
154154

155+
assert(m_running || m_scriptsToRemove.empty());
156+
155157
for (auto script : m_scriptsToRemove) {
156158
size_t index = -1;
157159
for (size_t i = 0; i < m_runningScripts.size(); i++) {
@@ -169,6 +171,7 @@ void Engine::frame()
169171
void Engine::start()
170172
{
171173
m_timer->reset();
174+
m_running = true;
172175

173176
for (auto target : m_targets) {
174177
auto gfBlocks = target->greenFlagBlocks();
@@ -180,6 +183,8 @@ void Engine::start()
180183
void Engine::stop()
181184
{
182185
m_runningScripts.clear();
186+
m_scriptsToRemove.clear();
187+
m_running = false;
183188
}
184189

185190
void Engine::startScript(std::shared_ptr<Block> topLevelBlock, std::shared_ptr<Target> target)
@@ -344,6 +349,8 @@ void Engine::run()
344349

345350
lastFrameTime = currentTime;
346351
}
352+
353+
stop();
347354
}
348355

349356
double Engine::fps() const

src/engine/internal/engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class Engine : public IEngine
115115
double m_fps = 30; // default FPS
116116
std::chrono::milliseconds m_frameDuration; // will be computed in run()
117117

118+
bool m_running = false;
118119
bool m_breakFrame = false;
119120
bool m_skipFrame = false;
120121
bool m_lockFrame = false;

test/engine/engine_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,11 @@ TEST(EngineTest, Clones)
407407
ASSERT_EQ((*list)[i].toString(), "1 2"); // TODO: Change this to "12" after #188 is fixed
408408
}
409409
}
410+
411+
TEST(EngineTest, NoCrashAfterStop)
412+
{
413+
// Regtest for #186
414+
Project p("regtest_projects/186_crash_after_stop.sb3");
415+
ASSERT_TRUE(p.load());
416+
p.run();
417+
}
1 KB
Binary file not shown.

0 commit comments

Comments
 (0)