1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Projucer: Fix a crash in BinaryResources::browseForResource()

This commit is contained in:
ed 2021-10-11 09:46:42 +01:00
parent b3a6e796f9
commit 0de0a2648e
2 changed files with 37 additions and 15 deletions

View file

@ -131,26 +131,45 @@ void BinaryResources::browseForResource (const String& title,
chooser = std::make_unique<FileChooser> (title, fileToStartFrom, wildcard); chooser = std::make_unique<FileChooser> (title, fileToStartFrom, wildcard);
auto flags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles; auto flags = FileBrowserComponent::openMode | FileBrowserComponent::canSelectFiles;
chooser->launchAsync (flags, [this, resourceToReplace, callback] (const FileChooser& fc) chooser->launchAsync (flags, [safeThis = WeakReference<BinaryResources> { this },
resourceToReplace,
callback] (const FileChooser& fc)
{ {
if (fc.getResult() == File{}) if (safeThis == nullptr)
callback ({});
String name (resourceToReplace);
if (name.isEmpty())
name = findUniqueName (fc.getResult().getFileName());
if (! add (name, fc.getResult()))
{ {
AlertWindow::showMessageBoxAsync (MessageBoxIconType::WarningIcon, if (callback != nullptr)
TRANS("Adding Resource"), callback ({});
TRANS("Failed to load the file!"));
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);
}); });
} }

View file

@ -93,4 +93,7 @@ private:
JucerDocument* document; JucerDocument* document;
OwnedArray<BinaryResource> resources; OwnedArray<BinaryResource> resources;
std::unique_ptr<FileChooser> chooser; std::unique_ptr<FileChooser> chooser;
//==============================================================================
JUCE_DECLARE_WEAK_REFERENCEABLE (BinaryResources)
}; };