Skip to content

Commit

Permalink
#5345: Fix some crashes during destruction
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 4, 2020
1 parent a7b710e commit 710ac66
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
38 changes: 33 additions & 5 deletions radiant/uimanager/SoundShaderPreview.cpp
Expand Up @@ -29,7 +29,8 @@ namespace
SoundShaderPreview::SoundShaderPreview(wxWindow* parent) :
wxPanel(parent, wxID_ANY),
_listStore(new wxutil::TreeModel(_columns, true)),
_soundShader("")
_soundShader(""),
_isShuttingDown(false)
{
SetSizer(new wxBoxSizer(wxHORIZONTAL));

Expand Down Expand Up @@ -76,6 +77,12 @@ SoundShaderPreview::SoundShaderPreview(wxWindow* parent) :
update();
}

SoundShaderPreview::~SoundShaderPreview()
{
_isShuttingDown = true;
_durationQueries.clear();
}

wxSizer* SoundShaderPreview::createControlPanel(wxWindow* parent)
{
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
Expand Down Expand Up @@ -138,7 +145,7 @@ void SoundShaderPreview::update()
// Clear the current treeview model
_listStore->Clear();

_durationQueries.clear();
_durationQueries.clearPendingTasks();

// If the soundshader string is empty, desensitise the widgets
Enable(!_soundShader.empty());
Expand Down Expand Up @@ -286,15 +293,36 @@ void SoundShaderPreview::loadFileDurationAsync(const std::string& soundFile)
{
try
{
// Query
auto duration = GlobalSoundManager().getSoundFileDuration(soundFile);
// In case the duration has been calculated in the meantime
// check if there's still need for this task to run
float duration = -1.0f;
{
std::lock_guard<std::mutex> lock(_durationsLock);

// Store the duration in the local cache
auto existing = _durations.find(soundFile);

if (existing != _durations.end())
{
duration = existing->second;
}
}

if (duration < 0)
{
// Duration still unknown, run the query
duration = GlobalSoundManager().getSoundFileDuration(soundFile);

// Store the duration in the local cache
std::lock_guard<std::mutex> lock(_durationsLock);
_durations[soundFile] = duration;
}

if (_isShuttingDown)
{
// Don't dispatch anything if we're shutting down
return;
}

// Dispatch to UI thread when we're done
GetUserInterfaceModule().dispatch([this, soundFile, duration]()
{
Expand Down
4 changes: 4 additions & 0 deletions radiant/uimanager/SoundShaderPreview.h
Expand Up @@ -65,9 +65,13 @@ class SoundShaderPreview :
// Sound file lengths are queried asynchronously
util::TaskQueue _durationQueries;

bool _isShuttingDown;

public:
SoundShaderPreview(wxWindow* parent);

~SoundShaderPreview();

/** greebo: Sets the soundshader to preview.
* This updates the preview liststore and treeview.
*/
Expand Down

0 comments on commit 710ac66

Please sign in to comment.