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 memory leak for Option<SwiftType> #273

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/swift-bridge-ir/src/bridged_type/bridged_opaque_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,18 @@ impl BridgeableType for OpaqueForeignType {
HostLang::Swift => {
let ty = &self.ty;

// Here we are converting a Swift type from its Rust representation to its FFI
// representation.
// When we drop the Rust representation we do not want to free the backing Swift
// type since we are passing ownership to Swift.
// So, we are transitioning this Swift type from its
// `RustRepr -> FfiRepr -> SwiftRepr`.
// This means that the Swift side will be responsible for freeing the Swift type
// whenever it is done with it.
// Here we use `ManuallyDrop` to avoid freeing the Swift type.
quote! {
if let Some(val) = #expression {
let val = std::mem::ManuallyDrop::new(val);
val.0 as *mut super::#ty
} else {
std::ptr::null_mut()
Expand Down Expand Up @@ -434,7 +444,7 @@ impl BridgeableType for OpaqueForeignType {
format!("{{ if let val = {expression} {{ val.isOwned = false; return val.ptr }} else {{ return nil }} }}()", expression = expression,)
}
HostLang::Swift => {
format!("{{ if let val = {expression} {{ return Unmanaged.passRetained(val).retain().toOpaque() }} else {{ return nil }} }}()")
Copy link
Owner Author

Choose a reason for hiding this comment

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

passRetained already increments the retain count. We do not want to increment it twice.

format!("{{ if let val = {expression} {{ return Unmanaged.passRetained(val).toOpaque() }} else {{ return nil }} }}()")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ mod extern_rust_fn_return_option_opaque_swift_type {
#[export_name = "__swift_bridge__$some_function"]
pub extern "C" fn __swift_bridge__some_function() -> *mut super::SomeSwiftType {
if let Some(val) = super::some_function() {
let val = std::mem::ManuallyDrop::new(val);
val.0 as *mut super::SomeSwiftType
} else {
std::ptr::null_mut()
Expand Down Expand Up @@ -945,7 +946,7 @@ mod extern_rust_fn_with_option_opaque_swift_type_arg {
ExpectedSwiftCode::ContainsAfterTrim(
r#"
func some_function(_ arg: Optional<SomeSwiftType>) {
__swift_bridge__$some_function({ if let val = arg { return Unmanaged.passRetained(val).retain().toOpaque() } else { return nil } }())
__swift_bridge__$some_function({ if let val = arg { return Unmanaged.passRetained(val).toOpaque() } else { return nil } }())
}
"#,
)
Expand Down
Loading