Fix memory leaks caused by std::move on types with no move constructor#1448
Open
jnbooth wants to merge 7 commits into
Open
Fix memory leaks caused by std::move on types with no move constructor#1448jnbooth wants to merge 7 commits into
std::move on types with no move constructor#1448jnbooth wants to merge 7 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1448 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 75 75
Lines 13455 13455
=========================================
Hits 13455 13455 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
cxxuses theIsRelocatablestruct to detect whether a C++ type is safe to move bymemcpy. It permits only types that have trivial move constructors and no destructor.cxx-qt-libandcxx-qt-lib-extrassometimes override that check, like so:This is a violation of
cxx's assumptions, but it's valid if two conditions are met:QTypeInfo<MyType>::isRelocatablemust be true. This ensuresmemcpyis safe to use.MyTypemust have a move constructor (i.e.MyType(MyType&& other)). This ensures that if the type is passed from Rust into C++ by value, C++ will take ownership of it, and vice versa.The first condition can be checked at compile time with
static_assert(QTypeInfo<MyType>::isRelocatable), which is exactly whatcxx-qt-libandcxx-qt-lib-extrasdo. However, as far as I know, there is no way to check the second condition at compile time. Due to this,cxx-qt-libandcxx-qt-lib-extrasdeclareIsRelocatablefor several types that do not have move constructors:QFont,QMessageLogContext,QCommandLineOption, andQCommandLineParser. If any of these types are passed by value into C++, their reference counter will not decrement, which means they will never deallocate.However, when those invalid
IsRelocatabledeclarations are removed, another problem arises: it is no longer possible for Rust to bind to C++ functions that return those types, since those are also passing by value. That includes the FFI bindings we use forMyType::default(),MyType::clone(), and so on. The solution is to construct the types in-place usingnew (p) MyType(...), which is the C++ syntax for applying a constructor to the existing place in memory at pointer addressp, rather than constructing a new value on the stack and returning it. (cxxmakes heavy use of that syntax in generated code, which is how I learned about it.) So nowcommon.hhas a new constructor function:On the Rust side, those types now use the
crate::util::new_in_placehelper function to construct their values, which usesMaybeUninitunder the hood.So instead of this:
Corresponding implementations now do this:
It's a bit unwieldy, but I think it's the cleanest approach. The only other way to prevent memory leaks that I can think of is to wrap those types in
UniquePtrs, but that would be a significant breaking change for those types and wouldn't really be an improvement anyway (in my opinion). This way, it's all kept internal behind the API.EDIT: Also to note, this is how
cxxhandles it. When an initializer is defined in a bridge, this is the Rust side:And this is the C++ side: