From 8e7a413ec6f8cad6b0e97810c6e006dd4920df72 Mon Sep 17 00:00:00 2001 From: Jean Raby Date: Sun, 11 Jun 2023 13:17:26 -0400 Subject: [PATCH] Fix x11 display leak (#590) * Call XCloseDisplay in all return paths Add test to confirm XOpenDisplay leak is plugged * whitespace --- robot_info_test.go | 10 +++++++++- window/pub.h | 1 + window/window.h | 10 +++++++--- 3 files changed, 17 insertions(+), 4 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..8e0ff69 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; } } @@ -494,13 +498,13 @@ void close_window_by_Id(MData m_data){ char* get_main_title(){ // Check if the window is valid if (!is_valid()) { return "is_valid failed."; } - + return get_title_by_hand(mData); } char* get_title_by_pid(uintptr pid, int8_t isPid){ MData win = set_handle_pid(pid, isPid); - return get_title_by_hand(win); + return get_title_by_hand(win); } char* named(void *result) {