mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-14 00:14:18 +00:00
FileChooser: Fix win32 threading bug
The Win32NativeFileChooser was taking ownership of itself in its `Thread::run` implementation. This meant that sometimes the destructor of the file chooser thread would execute directly in `Thread::run`. Now, we explicitly transfer ownership into a function object which will run asynchronously on the main thread. This way, the file chooser thread will be stopped on the main thread.
This commit is contained in:
parent
d83b3f7716
commit
8ad5ea0cb1
1 changed files with 22 additions and 7 deletions
|
|
@ -420,18 +420,33 @@ private:
|
|||
|
||||
void run() override
|
||||
{
|
||||
// We use a functor rather than a lambda here because
|
||||
// we want to move ownership of the Ptr into the function
|
||||
// object, and C++11 doesn't support general lambda capture
|
||||
struct AsyncCallback
|
||||
{
|
||||
AsyncCallback (Ptr p, Array<URL> r)
|
||||
: ptr (std::move (p)),
|
||||
results (std::move (r)) {}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
ptr->results = std::move (results);
|
||||
|
||||
if (ptr->owner != nullptr)
|
||||
ptr->owner->exitModalState (ptr->results.size() > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
Ptr ptr;
|
||||
Array<URL> results;
|
||||
};
|
||||
|
||||
// as long as the thread is running, don't delete this class
|
||||
Ptr safeThis (this);
|
||||
threadHasReference.signal();
|
||||
|
||||
auto r = openDialog (true);
|
||||
MessageManager::callAsync ([safeThis, r]
|
||||
{
|
||||
safeThis->results = r;
|
||||
|
||||
if (safeThis->owner != nullptr)
|
||||
safeThis->owner->exitModalState (r.size() > 0 ? 1 : 0);
|
||||
});
|
||||
MessageManager::callAsync (AsyncCallback (std::move (safeThis), std::move (r)));
|
||||
}
|
||||
|
||||
static HashMap<HWND, Win32NativeFileChooser*>& getNativeDialogList()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue