nashaofu / display-info

Cross-platform get display info for MacOS、Windows、Linux, Like electron Display Object.

Home Page:https://docs.rs/display-info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Maximum number of clients reached" when running DisplayInfo::all() multiple times

mdacach opened this issue · comments

Hello, hope this message finds you well! First of all, thank you for your work. The screenshots crate is great.

I am running into an issue on screenshots crate which I've tracked up to here. There's a link to a video showing the issue at the bottom.

The Issue

When calling DisplayInfo::all() multiple times, the error "Maximum number of clients reached" shows up.

Possible Explanation

Each call to DisplayInfo::all() is creating a connection to X that is not being closed. This leads to too many connections, and X refuses a new one.

Example

This is a simple script which calls DisplayInfo::all() in a loop:
image

Eventually, this happens. Calls up to 178 were fine, but starting there, we have "Maximum number of clients reached".
image

This is a 3-minute video walking through the issue.

Unfortunately, I could not get it working. I tried adding some XCloseDisplay calls throughout the code, but it didn't work (segfaults and whatnot). I hope the video helps, and thank you once again for your work.

Resources

Script's source code:
https://github.com/mdacach/display_info_test/tree/main
Video explaining the issue:
https://www.dropbox.com/s/qseowr2pnetre4y/display_info_issue.mp4?dl=0
Function where I think the problem is:

pub fn get_all() -> Vec<DisplayInfo> {

截屏2022-07-06 23 58 06

The latest version should have fixed this problem

Works perfectly! Thank you very much.
May I ask, out of curiosity:
1 - What was exactly the problem? I could not solve it by just adding a XCloseDisplay call (the code double-freed, I think).
2 - Where do I find documentation regarding XRR functions such as XRRFreeScreenResources? I did not find anything on the web.

Works perfectly! Thank you very much. May I ask, out of curiosity: 1 - What was exactly the problem? I could not solve it by just adding a XCloseDisplay call (the code double-freed, I think). 2 - Where do I find documentation regarding XRR functions such as XRRFreeScreenResources? I did not find anything on the web.

  1. The problem caused by this commit, I found it would be released twice,So remove XCloseDisplay,the possible reason is that the following code releases XDisplay first.
    let resource_manager_string_cstring = CString::from_raw(XResourceManagerString(display_ptr));
    let resource_manager_string = resource_manager_string_cstring.to_string_lossy();
    let prefix = "Xft.dpi:\t";
    let xft_dpi = resource_manager_string
    .split("\n")
    .find(|str| str.starts_with(prefix))
    .map(|str| str.replace(prefix, ""))
    .map(|dpi| dpi.parse::<f32>().unwrap_or(96.0))
    .unwrap_or(96.0);
  2. Official documentation: https://www.x.org/releases/X11R7.5/doc/index.html.

Thank you!