Skip to content

Conversation

@deanlee
Copy link
Contributor

@deanlee deanlee commented Dec 4, 2025

Adds a CallbackManager that stores bound-method callbacks as weak references and automatically skips those whose objects have been garbage-collected. This prevents callback lists from keeping UI objects alive and avoids memory leaks.

Note: Clients no longer need to manually remove callbacks—dead callbacks are ignored automatically.

A separate PR can add a SingleCallbackManager for places (e.g., dialogs, buttons) that only need single weak-referenced callback.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 4, 2025

raylib UI Preview

All Screenshots

@sshane
Copy link
Contributor

sshane commented Dec 5, 2025

I introduced a new bug that wiped the wifi menu's back callback on hide in #36767 avoiding weakref. Weakref seems like the standard approach to callback/event listener setups in Python (our callbacks) @adeebshihadeh. Done well, you won't have to be careful about callbacks keeping a closed Widget in memory

https://blog.codingconfessions.com/i/172320360/weakset

def close_dialog(self):
gui_app.set_modal_overlay(None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to make everything methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because WeakSet only supports weakly-referenced bound methods, adding lambdas or plain functions would complicate the design. Supporting bound-method callbacks is sufficient here, and we can use a separate class when we need more flexible callback types for a single-callback case.

@sshane
Copy link
Contributor

sshane commented Dec 5, 2025

Does this handle single callbacks added everywhere? For ex. the keyboard using 500K every time you open it from the back callback I assume

@deanlee
Copy link
Contributor Author

deanlee commented Dec 5, 2025

Does this handle single callbacks added everywhere? For ex. the keyboard using 500K every time you open it from the back callback I assume

Here is a draft for single callbacks: #36788. Supports bound methods, functions, and lambdas.

@deanlee deanlee force-pushed the ui-callback-manager branch from 7945f2a to 857ea97 Compare December 6, 2025 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants