From 0de0a2648e1cc27aad9a82e8e5f7f890e2594194 Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 11 Oct 2021 09:46:42 +0100 Subject: [PATCH] Projucer: Fix a crash in BinaryResources::browseForResource() --- .../ComponentEditor/jucer_BinaryResources.cpp | 49 +++++++++++++------ .../ComponentEditor/jucer_BinaryResources.h | 3 ++ 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/extras/Projucer/Source/ComponentEditor/jucer_BinaryResources.cpp b/extras/Projucer/Source/ComponentEditor/jucer_BinaryResources.cpp index 485284d629..9f81596d27 100644 --- a/extras/Projucer/Source/ComponentEditor/jucer_BinaryResources.cpp +++ b/extras/Projucer/Source/ComponentEditor/jucer_BinaryResources.cpp @@ -131,26 +131,45 @@ void BinaryResources::browseForResource (const String& title, chooser = std::make_unique (title, fileToStartFrom, wildcard); auto flags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles; - chooser->launchAsync (flags, [this, resourceToReplace, callback] (const FileChooser& fc) + chooser->launchAsync (flags, [safeThis = WeakReference { this }, + resourceToReplace, + callback] (const FileChooser& fc) { - if (fc.getResult() == File{}) - callback ({}); - - String name (resourceToReplace); - - if (name.isEmpty()) - name = findUniqueName (fc.getResult().getFileName()); - - if (! add (name, fc.getResult())) + if (safeThis == nullptr) { - AlertWindow::showMessageBoxAsync (MessageBoxIconType::WarningIcon, - TRANS("Adding Resource"), - TRANS("Failed to load the file!")); + if (callback != nullptr) + callback ({}); - name.clear(); + return; } - callback (name); + const auto result = fc.getResult(); + + auto resourceName = [safeThis, result, resourceToReplace]() -> String + { + if (result == File()) + return {}; + + if (resourceToReplace.isEmpty()) + return safeThis->findUniqueName (result.getFileName()); + + return resourceToReplace; + }(); + + if (resourceName.isNotEmpty()) + { + if (! safeThis->add (resourceName, result)) + { + AlertWindow::showMessageBoxAsync (MessageBoxIconType::WarningIcon, + TRANS("Adding Resource"), + TRANS("Failed to load the file!")); + + resourceName.clear(); + } + } + + if (callback != nullptr) + callback (resourceName); }); } diff --git a/extras/Projucer/Source/ComponentEditor/jucer_BinaryResources.h b/extras/Projucer/Source/ComponentEditor/jucer_BinaryResources.h index 2f2ef9cd32..733b1c8c96 100644 --- a/extras/Projucer/Source/ComponentEditor/jucer_BinaryResources.h +++ b/extras/Projucer/Source/ComponentEditor/jucer_BinaryResources.h @@ -93,4 +93,7 @@ private: JucerDocument* document; OwnedArray resources; std::unique_ptr chooser; + + //============================================================================== + JUCE_DECLARE_WEAK_REFERENCEABLE (BinaryResources) };