Skip to content

close #6: correctly free allocated memory #7

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

PiMaker
Copy link
Contributor

@PiMaker PiMaker commented Nov 2, 2020

Thanks for merging my previous PR! Here's a followup for #6.

I added to_c_string as an ugly workaround, and yes, it indeed does require a manual free. Turns out, the parser itself does too. Not a fan of intermingling rust memory allocation and c++ one, so I added seperate "free" functions to the lib that call delete.

To test, I extended the "policy" example and added some stuff to the simple API to ensure the new code paths are taken. Then I ran "cargo valgrind", verifying that there are indeed no memory leaks now.

Valgrind also showed my that the ones you had marked *mut c_char previously were in fact automatically released, so I changed them to "const" and updated the comment. I hope that's correct and libapt isn't somehow stupid about that too :)

I suppose a impl Drop wrapper for the returned string data (potentially Deref<str> or smth) would work too, but I figured we copy the string once, so might as well copy it twice and make our lives easier by just having Rust do the final free.

And extend policy example by printing short description for each version
found, to test it.
Adjust return type of ver_iter_* to be *const c_char, as that data does
not need manual freeing.

ver_file_parser_* function do require it (they use to_c_string) and the
parser itself also needs to be freed. Add a SafeVerFileParser type to
free the parser on drop, and free the data returned by the parser ASAP
after cloning into a Rust String.

Data is allocated with C++ 'new', so we add custom functions to call
'delete' instead of relying on libc::free to avoid any potential issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant