tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
82.67k stars 2.48k forks source link

[bug] on macOS call systray set_icon will cause memory leak #5382

Open zangdale opened 1 year ago

zangdale commented 1 year ago

Describe the bug

I want to change the icon on notifications area, I found that each calling method "app.tray_handle().set_icon(tauri::Icon::Raw(include_bytes!("../icons/2.png").to_vec())).unwrap();" will increase memory.

loop {
                    let icon_row = icons.get(i % icons_len);
                    if let Some(v) = icon_row {
                        app.tray_handle().set_icon(v.clone()).unwrap();
                    } else {
                        println!("Failed to open the file.");
                    }
                    i = i + 1;
                    if i == icons_len {
                        i = 0;
                    }
                    thread::sleep(ten_millis);
}

Reproduction

Continuous calls “set_icon()”

Expected behavior

No memory leaks

Platform and versions

cargo tauri info

Environment › OS: Mac OS 12.6.0 X64 › Node.js: 16.17.0 › npm: 8.15.0 › pnpm: Not installed! › yarn: 1.22.19 › rustup: 1.25.1 › rustc: 1.63.0 › cargo: 1.63.0 › Rust toolchain: stable-x86_64-apple-darwin

Packages › @tauri-apps/cli [NPM]: 1.1.1 › @tauri-apps/api [NPM]: 1.1.0 › tauri [RUST]: 1.1.1, › tauri-build [RUST]: 1.1.0, › tao [RUST]: 0.14.0, › wry [RUST]: 0.21.1,

App › build-type: bundle › CSP: unset › distDir: ../dist › devPath: http://localhost:1420/ › framework: Vue.js

App directory structure ├─ dist ├─ resources ├─ node_modules ├─ public ├─ runcat.app ├─ src-tauri ├─ .git ├─ .vscode ├─ .idea └─ src

Stack trace

No response

Additional context

Other languages have similar problems: https://github.com/getlantern/systray/issues/135

zangdale commented 1 year ago
图片 图片 图片
paulzql commented 1 year ago

same issue for me

haselkern commented 9 months ago

This is not only limited to set_icon. I'm noticing similar behavior when calling set_title and set_menu.

dustinblackman commented 4 months ago

Currently able to repro on MacOS using Tauri 1.6.7, where each update I push to my tray adds nearly 1MB of memory that never frees. It's unclear why values passed in to set_icon + friends do not properly drop memory after they're replaced. I've dug a bit and haven't found anything so far. I'm wondering if this is happening outside of rust and in the OS bindings instead?

dustinblackman commented 1 month ago

Digging deeper it looks like this may be the cause:

https://github.com/SSheldon/rust-objc/issues/80

Possible alternative here that may solve this in the future https://github.com/tauri-apps/wry/issues/1239

Specifically for tray icon: https://github.com/tauri-apps/tray-icon/pull/172