Skip to content

Commit c648fc7

Browse files
authoredAug 29, 2024··
C#: do not drop resource handles in finalizers (#1040)
* 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 #1039 Signed-off-by: Joel Dice <joel.dice@fermyon.com> * remove finalizers per PR feedback Signed-off-by: Joel Dice <joel.dice@fermyon.com> --------- Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 02f0784 commit c648fc7

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed
 

‎crates/csharp/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,11 @@ impl InterfaceGenerator<'_> {
14441444
.map(|key| self.resolve.name_world_key(key))
14451445
.unwrap_or_else(|| "$root".into());
14461446

1447+
// As of this writing, we cannot safely drop a handle to an imported resource from a .NET finalizer
1448+
// because it may still have one or more open child resources. Once WIT has explicit syntax for
1449+
// indicating parent/child relationships, we should be able to use that information to keep track
1450+
// of child resources automatically in generated code, at which point we'll be able to drop them in
1451+
// the correct order from finalizers.
14471452
uwriteln!(
14481453
self.src,
14491454
r#"
@@ -1458,22 +1463,17 @@ impl InterfaceGenerator<'_> {
14581463
14591464
public void Dispose() {{
14601465
Dispose(true);
1461-
GC.SuppressFinalize(this);
14621466
}}
14631467
14641468
[DllImport("{module_name}", EntryPoint = "[resource-drop]{name}"), WasmImportLinkage]
14651469
private static extern void wasmImportResourceDrop(int p0);
14661470
14671471
protected virtual void Dispose(bool disposing) {{
1468-
if (Handle != 0) {{
1472+
if (disposing && Handle != 0) {{
14691473
wasmImportResourceDrop(Handle);
14701474
Handle = 0;
14711475
}}
14721476
}}
1473-
1474-
~{upper_camel}() {{
1475-
Dispose(false);
1476-
}}
14771477
"#
14781478
);
14791479
}

0 commit comments

Comments
 (0)
Please sign in to comment.