-
Notifications
You must be signed in to change notification settings - Fork 30
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
Decrement ARC counts on SRObject drop #2
Comments
Did some testing and it doesn't seem like this is possible |
@Brendonovich did you figure out how to release Swift memory from Rust? |
@0rvar No, I wasn't able to figure it out. I was able to call the release and retain functions, but often the retain count of objects would be in the thousands and I had no idea why. No matter what I tried, dropping an SRObject wouldn't trigger the corresponding Swift object's deinit(). Would be great to figure it out but I'm not sure I want to try again haha. |
@Brendonovich that's too bad. Played around with |
@0rvar My curiosity got the better of me and I tried a different approach for doing retains and releases: Instead of doing so via those Rust bindings, I defined functions on the Swift side that use @_cdecl("decrease_retain_count")
public func decreaseRetainCount(obj: UnsafePointer<AnyObject>) {
let _ = Unmanaged.passUnretained(obj.pointee).release();
} and call it like this: extern "C" {
fn decrease_retain_count(obj: &SRString);
}
impl Drop for SRString {
fn drop(&mut self) {
unsafe { decrease_retain_count(&self) }
println!("Dropped SRString");
}
} Works just as I'd expect, printing the Drop message as well as the deinit() message! Knowing this I may just do a bit of a rewrite and give this library some much needed attention. |
@Brendonovich I've upgraded to latest master, but still getting memory leaking. I'll create a branch reproducing the leaks I get. EDIT: Leaking loads when running examples in this branch: https://github.com/0rvar/swift-rs/tree/orvar/memory-leaks |
@0rvar That's interesting. I haven't actually been measuring for memory leaks only checking that objects are being released properly. Building your example without the final loop, navigating to the executable's folder and running |
@Brendonovich I'm just running the program with the loop and looking at memory usage in Activity Monitor |
@0rvar Hmm, Instruments doesn't report anything wrong for some reason. I did the loop for 30000 iterations and then paused to allow for swift to potentially deallocate stuff but nope it just stays there. No idea why this would be happening |
After looking over swift-bindgen's code, it seems that it's possible to manually alter ARC counts for arbitrary pointers, meaning that it should be possible to let the swift runtime know when rust is done with an SRObject by decrementing the corresponding ARC. This should avoid memory leaks that I believe exist at the moment, since I don't thing swift knows when rust is finished with a value.
The text was updated successfully, but these errors were encountered: