-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
testtray: removing a tray item makes the test executable crash #12002
Comments
@Semphriss, can you take a look? |
Turns out I had forgotten to initialize a variable. Specifically, the one that stores the parent entry of a submenu. I'll push a fix in a few moments. |
testtray behaves as expected now on Windows 10. I had to apply this patch to satisfy --- a/test/testtray.c
+++ b/test/testtray.c
@@ -638,6 +638,7 @@ clean_window:
}
quit:
+ SDL_free(trays);
SDL_Quit();
SDLTest_CommonDestroyState(state);
Is this ok to apply @Semphriss ? Nevermind, no. I get a failure when closing the tray first and then the window. |
You're right, I forgot to free it at the end. Normally, I can just add: if (!trays_destroyed) {
SDL_free(tray);
} ... right before the |
Suggested patch, using a custom event. Use a custom event--- a/test/testtray.c
+++ b/test/testtray.c
@@ -2,6 +2,8 @@
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
+static Uint32 close_tray_event;
+
static void SDLCALL tray_quit(void *ptr, SDL_TrayEntry *entry)
{
SDL_Event e;
@@ -9,18 +11,11 @@ static void SDLCALL tray_quit(void *ptr, SDL_TrayEntry *entry)
SDL_PushEvent(&e);
}
-static bool trays_destroyed = false;
-
static void SDLCALL tray_close(void *ptr, SDL_TrayEntry *entry)
{
- SDL_Tray **trays = (SDL_Tray **) ptr;
-
- trays_destroyed = true;
-
- SDL_DestroyTray(trays[0]);
- SDL_DestroyTray(trays[1]);
-
- SDL_free(trays);
+ SDL_Event e;
+ e.type = close_tray_event;
+ SDL_PushEvent(&e);
}
static void SDLCALL apply_icon(void *ptr, const char * const *filelist, int filter)
@@ -49,7 +44,7 @@ static void SDLCALL change_icon(void *ptr, SDL_TrayEntry *entry)
{ "All files", "*" },
};
- SDL_ShowOpenFileDialog(apply_icon, ptr, NULL, filters, 2, NULL, 0);
+ SDL_ShowOpenFileDialog(apply_icon, ptr, NULL, filters, SDL_arraysize(filters), NULL, 0);
}
static void SDLCALL print_entry(void *ptr, SDL_TrayEntry *entry)
@@ -107,6 +102,8 @@ static void SDLCALL append_button_to(void *ptr, SDL_TrayEntry *entry)
SDL_TrayEntry *new_ctrl_disabled;
SDL_TrayEntry *new_example;
+ close_tray_event = SDL_RegisterEvents(1);
+
new_ctrl = SDL_InsertTrayEntryAt(SDL_GetTrayEntryParent(entry), -1, "New button", SDL_TRAYENTRY_SUBMENU);
if (!new_ctrl) {
@@ -569,17 +566,8 @@ int main(int argc, char **argv)
SDL_TrayEntry *entry_close = SDL_InsertTrayEntryAt(menu, -1, "Close", SDL_TRAYENTRY_BUTTON);
CHECK(entry_close);
- /* TODO: Track memory! */
- SDL_Tray **trays = SDL_malloc(sizeof(SDL_Tray *) * 2);
- if (!trays) {
- goto clean_all;
- }
-
- trays[0] = tray;
- trays[1] = tray2;
-
SDL_SetTrayEntryCallback(entry_quit, tray_quit, NULL);
- SDL_SetTrayEntryCallback(entry_close, tray_close, trays);
+ SDL_SetTrayEntryCallback(entry_close, tray_close, NULL);
SDL_InsertTrayEntryAt(menu, -1, NULL, 0);
@@ -619,18 +607,18 @@ int main(int argc, char **argv)
} else if (e.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) {
SDL_DestroyWindow(w);
w = NULL;
+ } else if (e.type == close_tray_event) {
+ SDL_DestroyTray(tray);
+ SDL_DestroyTray(tray2);
+ tray = NULL;
+ tray2 = NULL;
}
}
clean_all:
- if (!trays_destroyed) {
- SDL_DestroyTray(tray2);
- }
-
+ SDL_DestroyTray(tray2);
clean_tray1:
- if (!trays_destroyed) {
- SDL_DestroyTray(tray);
- }
+ SDL_DestroyTray(tray);
clean_window:
if (w) { |
I've tried your patch but I'm getting occasional segfaults when closing the tray; I'll look into it sometime later tonight. |
I can easily crash testtray on Windows 10 by removing a tray item.
Crashing scenario 1:
Crashing scenario 2:
Run testtray
Click on "Tray control menu" -> "Create Button"
Click on "Tray control menu" -> "New Button" -> "Remove"
The following message is printed on the terminal:
Click on "Tray control menu" -> "New Button" -> "Remove" (try to remove item 2nd time)
Segmentation fault
The text was updated successfully, but these errors were encountered: