Fix x11 display leak (#590)

* Call XCloseDisplay in all return paths

Add test to confirm XOpenDisplay leak is plugged

* whitespace
This commit is contained in:
Jean Raby 2023-06-11 13:17:26 -04:00 committed by GitHub
parent 8ce3a5dff0
commit 8e7a413ec6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View File

@ -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()
}
}

View File

@ -191,6 +191,7 @@ typedef struct _Bounds Bounds;
if (items != NULL) {
*items = (uint32_t) nItems;
}
XCloseDisplay(rDisplay);
return result;
}
}

View File

@ -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) {