Skip to content

Commit 049fc34

Browse files
committed
Remove stopped threads in blocks implementation
1 parent 85f8b13 commit 049fc34

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

src/blocks/looksblocks.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ void LooksBlocks::registerBlocks(IEngine *engine)
8585
engine->addFieldValue(this, "backward", Backward);
8686
}
8787

88+
void LooksBlocks::onInit(IEngine *engine)
89+
{
90+
engine->threadAboutToStop().connect([](VirtualMachine *vm) {
91+
m_timeMap.erase(vm);
92+
93+
for (auto it = m_waitingBubbles.begin(); it != m_waitingBubbles.end();) {
94+
if (it->second == vm)
95+
m_waitingBubbles.erase(it);
96+
else
97+
it++;
98+
}
99+
});
100+
}
101+
88102
void LooksBlocks::compileSayForSecs(Compiler *compiler)
89103
{
90104
compiler->addInput(MESSAGE);

src/blocks/looksblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class LooksBlocks : public IBlockSection
6161
std::string name() const override;
6262

6363
void registerBlocks(IEngine *engine) override;
64+
void onInit(IEngine *engine) override;
6465

6566
static void compileSayForSecs(Compiler *compiler);
6667
static void compileSay(Compiler *compiler);

src/blocks/motionblocks.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ void MotionBlocks::registerBlocks(IEngine *engine)
7171
engine->addFieldValue(this, "all around", AllAround);
7272
}
7373

74+
void MotionBlocks::onInit(IEngine *engine)
75+
{
76+
engine->threadAboutToStop().connect([](VirtualMachine *vm) {
77+
m_timeMap.erase(vm);
78+
m_glideMap.erase(vm);
79+
});
80+
}
81+
7482
void MotionBlocks::compileMoveSteps(Compiler *compiler)
7583
{
7684
compiler->addInput(STEPS);

src/blocks/motionblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class MotionBlocks : public IBlockSection
4747
std::string name() const override;
4848

4949
void registerBlocks(IEngine *engine) override;
50+
void onInit(IEngine *engine) override;
5051

5152
static void compileMoveSteps(Compiler *compiler);
5253
static void compileTurnRight(Compiler *compiler);

src/blocks/soundblocks.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ void SoundBlocks::registerBlocks(IEngine *engine)
3636
void SoundBlocks::onInit(IEngine *engine)
3737
{
3838
m_waitingSounds.clear();
39-
// TODO: Remove stopped threads from m_waitingSounds
39+
40+
engine->threadAboutToStop().connect([](VirtualMachine *vm) {
41+
for (auto it = m_waitingSounds.begin(); it != m_waitingSounds.end();) {
42+
if (it->second == vm)
43+
m_waitingSounds.erase(it);
44+
else
45+
it++;
46+
}
47+
});
4048
}
4149

4250
bool SoundBlocks::compilePlayCommon(Compiler *compiler, bool untilDone, bool *byIndex)

0 commit comments

Comments
 (0)