Skip to content
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

Fix double-free when freeing ArgInfoList objects #58

Merged
merged 1 commit into from
Jun 28, 2022
Merged

Conversation

staticfloat
Copy link
Contributor

We previously used StringList() as a convenient way to print out the
options of an ArgInfoList, however StringList() by default assumes
that it owns the memory and should free it when the object is destroyed.
This conflicted with the ArgInfoList's own cleanup. This PR fixes the
issue by adding an owned keyword argument to StringList and using it
within the show() override. It also includes a test that should
purposefully trigger the double-free, to prevent future regressions.

We previously used `StringList()` as a convenient way to print out the
options of an `ArgInfoList`, however `StringList()` by default assumes
that it owns the memory and should free it when the object is destroyed.
This conflicted with the `ArgInfoList`'s own cleanup.  This PR fixes the
issue by adding an `owned` keyword argument to `StringList` and using it
within the `show()` override.  It also includes a test that should
purposefully trigger the double-free, to prevent future regressions.
end
end

SoapySDRArgInfoList_clear(s::ArgInfoList) = @GC.preserve s SoapySDRArgInfoList_clear(pointer_from_objref(s), s.length)
SoapySDRArgInfoList_clear(s::ArgInfoList) = @GC.preserve s SoapySDRArgInfoList_clear(s.ptr, s.length)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am never certain on the correct use of @GC.preserve. It looks like if we omit this convenience call we can avoid it all together, like: https://github.com/JuliaTelecom/SoapySDR.jl/blame/61e3793c3fa543d7e0139956dc346d248e76fc17/src/typewrappers.jl#L73

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line isn't changing anything about the @GC.preserve, it's instead just eliminating the slightly-confusing pointer_from_objref(s) and using the embedded pointer directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. What I mean is that we could call SoapySDRArgInfoList_clear(s.ptr, s.length) directly in the finalizer to avoid the need for a @GC.preserve all together.

@sjkelly sjkelly merged commit a875626 into main Jun 28, 2022
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.

2 participants