Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nullptr check when closing windows #23024

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 25 additions & 7 deletions shell/browser/window_list.cc
Expand Up @@ -10,6 +10,18 @@
#include "shell/browser/native_window.h"
#include "shell/browser/window_list_observer.h"

namespace {
template <typename T>
std::vector<base::WeakPtr<T>> ConvertToWeakPtrVector(std::vector<T*> raw_ptrs) {
std::vector<base::WeakPtr<T>> converted_to_weak;
converted_to_weak.reserve(raw_ptrs.size());
for (auto* raw_ptr : raw_ptrs) {
converted_to_weak.push_back(raw_ptr->GetWeakPtr());
}
return converted_to_weak;
}
} // namespace

namespace electron {

// static
Expand Down Expand Up @@ -80,20 +92,26 @@ void WindowList::RemoveObserver(WindowListObserver* observer) {

// static
void WindowList::CloseAllWindows() {
WindowVector windows = GetInstance()->windows_;
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
ConvertToWeakPtrVector(GetInstance()->windows_);
#if defined(OS_MACOSX)
std::reverse(windows.begin(), windows.end());
std::reverse(weak_windows.begin(), weak_windows.end());
#endif
for (auto* const& window : windows)
if (!window->IsClosed())
for (const auto& window : weak_windows) {
if (window && !window->IsClosed())
window->Close();
}
}

// static
void WindowList::DestroyAllWindows() {
WindowVector windows = GetInstance()->windows_;
for (auto* const& window : windows)
window->CloseImmediately(); // e.g. Destroy()
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
ConvertToWeakPtrVector(GetInstance()->windows_);

for (const auto& window : weak_windows) {
if (window)
window->CloseImmediately();
}
}

WindowList::WindowList() {}
Expand Down