Fix a leak in MacOS thread count function (#34862)

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2025-01-06 19:28:30 -08:00 committed by GitHub
parent b6a5eaa3db
commit d252a631d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 1 deletions

View file

@ -56,6 +56,7 @@ fn main() -> Result<(), Box<dyn Error>> {
#[cfg(not(windows))]
panic!("Cross-compiling to windows is currently not supported");
} else if target_os == "macos" {
println!("cargo:rerun-if-changed=platform/macos/count_threads.c");
cc::Build::new()
.file("platform/macos/count_threads.c")
.compile("count_threads");

View file

@ -8,6 +8,13 @@ int macos_count_running_threads() {
task_t task = current_task();
thread_act_array_t threads;
mach_msg_type_number_t tcnt;
task_threads(task, &threads, &tcnt);
const kern_return_t status = task_threads(task, &threads, &tcnt);
if (status == KERN_SUCCESS) {
// Free data structures attached to the thread list.
for (uint32_t t = 0; t < tcnt; t++) {
mach_port_deallocate(task, threads[t]);
}
vm_deallocate(task, (vm_address_t)threads, sizeof(thread_t) * tcnt);
}
return tcnt;
}