-
Notifications
You must be signed in to change notification settings - Fork 57
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 leaks when passing Drop objects to msg_send! #80
Comments
Thanks for the example @scoopr! So let's look at this part of the example: let obj = test_utils::WrappedObject::new();
unsafe { msg_send![..., isEqual:obj]; } Rust and the objc crate have no idea what types are supposed to be passed to unsafe extern fn isEqual(self: *mut Object, cmd: Sel, object: WrappedObject) For a function with this signature, the But Another potential danger here is that the Objective-C method implementation is expecting a pointer to an object, and instead we're giving it a Rust struct. In this case it happens that the layout of the Rust struct is exactly a pointer, but if it has any other fields, we could be in trouble. So I'd say when writing this unsafe code, we must ensure that we're not passing any non- To avoid this, you can write your code like: msg_send![..., isEqual:&*obj] Or, even better, you could write a little wrapper function that enforces the types you expect: impl WrappedObjectRef {
unsafe fn isEqual(&self, object: &WrappedObjectRef) -> BOOL {
msg_send![self, isEqual:object]
}
} With this wrapper function, the code would report:
I'm trying to think if there's ways the
For now though this is one of the things that needs to be manually audited when writing Does that all make sense and solve your issue? Let me know if you've got any other ideas about this. |
I would very much like that there would be some enforcement that leaks wouldn't be quite as easy. I mean, First, I'm still not understanding what happens to the arguments. They are captured as.. tuples? which then implement One other solution I was thinking was having the I can't quite wrap my head around what kinds of dereferencing or inferring would/do happen in all this, so it may be I don't see the forest from the trees here. |
It sounds to me that
Perhaps, we can solve one thing at a time? |
This is further investigation on SSheldon/rust-objc-foundation#9
I made an additional test to rust-objc that shows the issue here scoopr@b637c96.
It has a minimal wrapper around the type that implements
Drop
, and then when used as an argument tomsg_send!
, it seems the drop is then never called. Using it just as a receiver for a method call does seem to work as expected.I'm still not sure if the wrapper is wrong, or if there is a bug in
msg_send!
, or something else, but the fact is that there already is bunch of code that expects the drop to happen, and it would be really nice to have a solution without leaks :)The text was updated successfully, but these errors were encountered: