-
Notifications
You must be signed in to change notification settings - Fork 15
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
[question] Memory management with C bindings #226
Comments
I'm looking into this now. Assuming I understand what's happening under the hood correctly, which might be a big assumption, C should have full ownership over the struct of pointers in Rust's borrow-checker parlance, but I don't know what manually freeing that memory would look like. I can play around with an example and get back to you with what I find out. |
It looks like the correct way to do it is to provide a function in the library that disposes of memory using Rust's manual memory management. If I just use free on the raw pointers in C, valgrind says the memory leak does disappear, but it also complains that the free() calls are invalid. |
@logantm2 I think I figured out what needs to happen. So, I was under the assumption that you would be able to free that memory from C since I transferred ownership, but it looks like that while it may work sometimes due to interoperability between C's allocator and Rust's standard allocator, it's not guaranteed to work and is unspecified behavior. So, I'm working on adding deallocation functions to call from C that appropriately dispose of structs that contain raw pointers originally created in Rust. It will take a little time to get everything working, but for future reference they will look like this:
And will be called like this:
This makes valgrind happy:
|
A neater way to do this would be to just implement |
Ah, this explains why I was getting intermittent errors when trying to use |
@logantm2 Everything seems to work, so try downloading the dev branch and adding |
Thanks again for looking into this. I'm just now getting around to putting this into hpic2. It looks like it's segfaulting on the |
I'm also thinking about how to implement this in such a way that we use it where possible, but don't break installations when people have old versions of RustBCA and new versions of hPIC2. Is it feasible to add a macro or something similar in |
Yeah, that's very possible - this is my first time interacting with Rust's deallocator, so I wouldn't be surprised if there's gotchas (especially since all the magic happens in an
I don't know of any way to do this; it looks like the way other people do it is by specifying version numbers in the header file and then using the preprocessor to selectively compile stuff. If I added something to the header file though it wouldn't help, since a user with an old version would have to download a new version to get the header file... If you can figure anything out for this issue, I'd be happy to try to implement it on my side. I'll look into it as well, but I'm not even sure what to search for, haha. |
I was able to reproduce the segfault; adding a check that length is nonzero seems to have fixed it. That change is on the dev branch now. |
It actually wasn't as hard as I expected to check if the function exists on our end, so I think we're good to go! |
In the C bindings, the Output structs contain raw pointers to data arrays. How can we delete these arrays once we are done with them? I suspect this is causing a memory leak in some of our bigger problems.
The text was updated successfully, but these errors were encountered: