Skip to content

Commit 54f8c4e

Browse files
authored
fix: nullptr check when closing windows (electron#22948)
1 parent a7469f8 commit 54f8c4e

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

shell/browser/window_list.cc

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@
1010
#include "shell/browser/native_window.h"
1111
#include "shell/browser/window_list_observer.h"
1212

13+
namespace {
14+
template <typename T>
15+
std::vector<base::WeakPtr<T>> ConvertToWeakPtrVector(std::vector<T*> raw_ptrs) {
16+
std::vector<base::WeakPtr<T>> converted_to_weak;
17+
converted_to_weak.reserve(raw_ptrs.size());
18+
for (auto* raw_ptr : raw_ptrs) {
19+
converted_to_weak.push_back(raw_ptr->GetWeakPtr());
20+
}
21+
return converted_to_weak;
22+
}
23+
} // namespace
24+
1325
namespace electron {
1426

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

8193
// static
8294
void WindowList::CloseAllWindows() {
83-
WindowVector windows = GetInstance()->windows_;
95+
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
96+
ConvertToWeakPtrVector(GetInstance()->windows_);
8497
#if defined(OS_MACOSX)
85-
std::reverse(windows.begin(), windows.end());
98+
std::reverse(weak_windows.begin(), weak_windows.end());
8699
#endif
87-
for (auto* const& window : windows)
88-
if (!window->IsClosed())
100+
for (const auto& window : weak_windows) {
101+
if (window && !window->IsClosed())
89102
window->Close();
103+
}
90104
}
91105

92106
// static
93107
void WindowList::DestroyAllWindows() {
94-
WindowVector windows = GetInstance()->windows_;
95-
for (auto* const& window : windows)
96-
window->CloseImmediately(); // e.g. Destroy()
108+
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
109+
ConvertToWeakPtrVector(GetInstance()->windows_);
110+
111+
for (const auto& window : weak_windows) {
112+
if (window)
113+
window->CloseImmediately();
114+
}
97115
}
98116

99117
WindowList::WindowList() = default;

0 commit comments

Comments
 (0)