From 1b1f1f123a074d2fa5e4f75082e24dfc78a5900f Mon Sep 17 00:00:00 2001 From: Jean Raby Date: Sat, 10 Jun 2023 19:51:29 -0400 Subject: [PATCH] Call XCloseDisplay in all return paths Add test to confirm XOpenDisplay leak is plugged --- robot_info_test.go | 10 +++++++++- window/pub.h | 1 + window/window.h | 6 +++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/robot_info_test.go b/robot_info_test.go index 81a244c..93a9eb3 100644 --- a/robot_info_test.go +++ b/robot_info_test.go @@ -43,5 +43,13 @@ func TestGetSysScale(t *testing.T) { log.Println("SysScale: ", s) f := robotgo.ScaleF() - log.Println("sclae: ", f) + log.Println("scale: ", f) +} + +func TestGetTitle(t *testing.T) { + // just exercise the function, it used to crash with a segfault + "Maximum + // number of clients reached" + for i := 0; i < 128; i++ { + robotgo.GetTitle() + } } diff --git a/window/pub.h b/window/pub.h index 2ab4462..af1158b 100644 --- a/window/pub.h +++ b/window/pub.h @@ -191,6 +191,7 @@ typedef struct _Bounds Bounds; if (items != NULL) { *items = (uint32_t) nItems; } + XCloseDisplay(rDisplay); return result; } } diff --git a/window/window.h b/window/window.h index 35eaef5..d5cec04 100644 --- a/window/window.h +++ b/window/window.h @@ -104,7 +104,10 @@ bool is_valid() { // Get the window PID property void* result = GetWindowProperty(mData, WM_PID,NULL); - if (result == NULL) { return false; } + if (result == NULL) { + XCloseDisplay(rDisplay); + return false; + } // Free result and return true XFree(result); @@ -405,6 +408,7 @@ MData get_active(void) { if (window != 0) { // Set and return the foreground window result.XWin = (Window)window; + XCloseDisplay(rDisplay); return result; } }