Skip to content

Commit

Permalink
Merge pull request #43 from scratchcpp/fix_early_close_event_loop
Browse files Browse the repository at this point in the history
Fix #41: Do not start the event loop during destruction of ProjectLoader
  • Loading branch information
adazem009 authored Dec 9, 2023
2 parents a81e0c6 + eea97ee commit 89a478d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ScratchCPPGui/projectloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ ProjectLoader::ProjectLoader(QObject *parent) :

ProjectLoader::~ProjectLoader()
{
m_stopLoading = true;

if (m_loadThread.isRunning())
m_loadThread.waitForFinished();

Expand Down Expand Up @@ -88,6 +90,7 @@ void ProjectLoader::setFileName(const QString &newFileName)
emit loadStatusChanged();
emit fileNameChanged();

m_stopLoading = false;
m_loadThread = QtConcurrent::run(&callLoad, this);
}

Expand Down Expand Up @@ -193,7 +196,8 @@ void ProjectLoader::load()

m_sprites.clear();

if (!m_engine) {
if (!m_engine || m_stopLoading) {
m_engineMutex.unlock();
emit fileNameChanged();
emit loadStatusChanged();
emit loadingFinished();
Expand Down Expand Up @@ -226,6 +230,16 @@ void ProjectLoader::load()
}
}

if (m_stopLoading) {
m_engineMutex.unlock();
emit fileNameChanged();
emit loadStatusChanged();
emit loadingFinished();
emit engineChanged();
emit spritesChanged();
return;
}

// Run event loop
m_engine->setSpriteFencingEnabled(false);

Expand Down
1 change: 1 addition & 0 deletions ScratchCPPGui/projectloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class ProjectLoader : public QObject
bool m_eventLoopEnabled = true;
std::atomic<unsigned int> m_downloadedAssets = 0;
std::atomic<unsigned int> m_assetCount = 0;
std::atomic<bool> m_stopLoading = false;
};

} // namespace scratchcppgui

0 comments on commit 89a478d

Please sign in to comment.