Skip to content
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

Memory leak? #62

Open
joncrangle opened this issue Oct 15, 2024 · 1 comment
Open

Memory leak? #62

joncrangle opened this issue Oct 15, 2024 · 1 comment

Comments

@joncrangle
Copy link

Howdy! I was using your crate to send mach messages with system stats to Sketchybar (Simple Sketchybar System Stats). I noticed that over time, the memory usage kept slowly growing.

I believe the main culprit is that the response pointer isn't freed after each message:

let result = unsafe {
        CStr::from_ptr(sketchybar(command.into_raw(), bar_name.into_raw()))
            .to_string_lossy()
            .into_owned()
    };

    Ok(result)

However, I'm no Rust or C expert (this is my first app using either language), so I iterated and tried different solutions to reduce the memory and port usage in my own implementation.

I ended up adding a C function to sketchybar.h to call from the Rust side to free the response pointer:

void free_sketchybar_response(char *response) { free(response); }

I don't know if you've noticed a similar issue with memory usage, but I'm sharing my implementation in case it's helpful:

sketchybar.rs
sketchybar.h

Some of my modifications are specific to my use case (I'm sending lots of messages, however I'm not expecting to receive messages), and I wanted to refresh the mach ports periodically because I had an issue with stale response ports not always being deallocated, so my function is async. I also haave a cleanup function in case my app is stopped but Sketchybar continues running.

@johnallen3d
Copy link
Owner

I think you're on to something @joncrangle! 😂
image

I'll incorporate your free_sketchybar_response function. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants