Skip to content

Commit f1f5e61

Browse files
committed
Fix crashes when removing bubble and sound info
1 parent 5269847 commit f1f5e61

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/blocks/looksblocks.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ using namespace libscratchcpp;
1919
IRandomGenerator *LooksBlocks::rng = nullptr;
2020
IClock *LooksBlocks::clock = nullptr;
2121

22+
// TODO: Use C++20
23+
template<typename ContainerT, typename PredicateT>
24+
void erase_if(ContainerT &items, const PredicateT &predicate)
25+
{
26+
for (auto it = items.begin(); it != items.end();) {
27+
if (predicate(*it))
28+
it = items.erase(it);
29+
else
30+
++it;
31+
}
32+
}
33+
2234
std::string LooksBlocks::name() const
2335
{
2436
return "Looks";
@@ -89,13 +101,7 @@ void LooksBlocks::onInit(IEngine *engine)
89101
{
90102
engine->threadAboutToStop().connect([](VirtualMachine *vm) {
91103
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-
}
104+
erase_if(m_waitingBubbles, [vm](const std::pair<Target *, VirtualMachine *> &pair) { return pair.second == vm; });
99105
});
100106
}
101107

src/blocks/soundblocks.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010

1111
using namespace libscratchcpp;
1212

13+
// TODO: Use C++20
14+
template<typename ContainerT, typename PredicateT>
15+
static void erase_if(ContainerT &items, const PredicateT &predicate)
16+
{
17+
for (auto it = items.begin(); it != items.end();) {
18+
if (predicate(*it))
19+
it = items.erase(it);
20+
else
21+
++it;
22+
}
23+
}
24+
1325
std::string SoundBlocks::name() const
1426
{
1527
return "Sound";
@@ -36,15 +48,7 @@ void SoundBlocks::registerBlocks(IEngine *engine)
3648
void SoundBlocks::onInit(IEngine *engine)
3749
{
3850
m_waitingSounds.clear();
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-
});
51+
engine->threadAboutToStop().connect([](VirtualMachine *vm) { erase_if(m_waitingSounds, [vm](const std::pair<Sound *, VirtualMachine *> &pair) { return pair.second == vm; }); });
4852
}
4953

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

0 commit comments

Comments
 (0)