Skip to content

Commit

Permalink
C#: do not drop resource handles in finalizers (bytecodealliance#1040)
Browse files Browse the repository at this point in the history
* C#: do not drop resource handles in finalizers

As of this writing, we cannot safely drop a handle to an imported resource from
a .NET finalizer because it may still have one or more open child resources.
Once WIT has explicit syntax for indicating parent/child relationships, we
should be able to use that information to keep track of child resources
automatically in generated code, at which point we'll be able to drop them in
the correct order from finalizers.

Fixes bytecodealliance#1039

Signed-off-by: Joel Dice <[email protected]>

* remove finalizers per PR feedback

Signed-off-by: Joel Dice <[email protected]>

---------

Signed-off-by: Joel Dice <[email protected]>
  • Loading branch information
dicej authored Aug 29, 2024
1 parent 02f0784 commit c648fc7
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,11 @@ impl InterfaceGenerator<'_> {
.map(|key| self.resolve.name_world_key(key))
.unwrap_or_else(|| "$root".into());

// As of this writing, we cannot safely drop a handle to an imported resource from a .NET finalizer
// because it may still have one or more open child resources. Once WIT has explicit syntax for
// indicating parent/child relationships, we should be able to use that information to keep track
// of child resources automatically in generated code, at which point we'll be able to drop them in
// the correct order from finalizers.
uwriteln!(
self.src,
r#"
Expand All @@ -1458,22 +1463,17 @@ impl InterfaceGenerator<'_> {
public void Dispose() {{
Dispose(true);
GC.SuppressFinalize(this);
}}
[DllImport("{module_name}", EntryPoint = "[resource-drop]{name}"), WasmImportLinkage]
private static extern void wasmImportResourceDrop(int p0);
protected virtual void Dispose(bool disposing) {{
if (Handle != 0) {{
if (disposing && Handle != 0) {{
wasmImportResourceDrop(Handle);
Handle = 0;
}}
}}
~{upper_camel}() {{
Dispose(false);
}}
"#
);
}
Expand Down

0 comments on commit c648fc7

Please sign in to comment.