1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:47:44 +00:00

WindowServer: Make WSButton a Weakable and stop rawly pointing to it.

We had a crash due to dereferencing a destroyed WSButton after clicking
a window close button. Avoid that problem by using WeakPtr.
This commit is contained in:
Andreas Kling 2019-04-06 21:16:41 +02:00
parent e74f32ae40
commit 0808d5158c
3 changed files with 11 additions and 5 deletions

View file

@ -3,13 +3,14 @@
#include <SharedGraphics/Rect.h>
#include <AK/Function.h>
#include <AK/Retained.h>
#include <AK/Weakable.h>
class CharacterBitmap;
class Painter;
class WSMouseEvent;
class WSWindowFrame;
class WSButton final {
class WSButton : public Weakable<WSButton> {
public:
WSButton(WSWindowFrame&, Retained<CharacterBitmap>&&, Function<void()>&& on_click_handler);
~WSButton();

View file

@ -621,6 +621,11 @@ bool WSWindowManager::process_ongoing_window_resize(const WSMouseEvent& event, W
return true;
}
void WSWindowManager::set_cursor_tracking_button(WSButton* button)
{
m_cursor_tracking_button = button ? button->make_weak_ptr() : nullptr;
}
void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*& event_window)
{
event_window = nullptr;
@ -1117,5 +1122,5 @@ const WSCursor& WSWindowManager::active_cursor() const
void WSWindowManager::set_hovered_button(WSButton* button)
{
m_hovered_button = button;
m_hovered_button = button ? button->make_weak_ptr() : nullptr;
}

View file

@ -100,7 +100,7 @@ public:
const WSCursor& move_cursor() const { return *m_move_cursor; }
void set_active_window(WSWindow*);
void set_cursor_tracking_button(WSButton* button) { m_cursor_tracking_button = button; }
void set_cursor_tracking_button(WSButton*);
void set_hovered_button(WSButton*);
private:
@ -207,8 +207,8 @@ private:
CircularQueue<float, 30> m_cpu_history;
String m_username;
WSButton* m_cursor_tracking_button { nullptr };
WSButton* m_hovered_button { nullptr };
WeakPtr<WSButton> m_cursor_tracking_button;
WeakPtr<WSButton> m_hovered_button;
};
template<typename Callback>