Skip to content

Commit

Permalink
[bgen] Make the generated code check for zero handle before creating …
Browse files Browse the repository at this point in the history
…an INativeObject wrapper instance. Fixes part of dotnet#18452.

Also don't create the instance directly from third-party bindings, the
required constructor might not be public - use Runtime.GetINativeObject
instead.

Fixes part 4 of dotnet#18452.
  • Loading branch information
rolfbjarne committed Jun 16, 2023
1 parent 959bb55 commit 93a1f52
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/bgen/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,10 @@ public TrampolineInfo MakeTrampoline (Type t)

if (TypeManager.INativeObject.IsAssignableFrom (pi.ParameterType)) {
pars.Add (new TrampolineParameterInfo (NativeHandleType, safe_name));
invoke.AppendFormat ("new {0} ({1}, false)", pi.ParameterType, safe_name);
if (BindThirdPartyLibrary)
invoke.AppendFormat ("Runtime.GetINativeObject<{0}> ({1}, false)!", pi.ParameterType, safe_name);
else
invoke.AppendFormat ("{1} == IntPtr.Zero ? null! : new {0} ({1}, false)", pi.ParameterType, safe_name);
continue;
}

Expand Down

0 comments on commit 93a1f52

Please sign in to comment.