C
C#5mo ago
Delta 2.1

I've loaded CoreClr from hostFxr and it looks like the garbage Collector is not doing his job...

I'm trying to build a safe interop with Rust and C#, And I wanted SafeHandle to manage native rust object in C#, but DestroyHandle() is never called (while all references to this object are out of scope). so I tried to make a basic objet with a finalizer (inheriting CriticalFinalizerObject) and this finalizer is never called too... My entry point is an unmanagedMethod, and CoreClr is loaded with the netCoreHost rust crate.
4 Replies
Delta 2.1
Delta 2.15mo ago
look managed unmanaged method never call the GC, I a third method (i've a setup a DLLresolver to my host executable, the main function) and now I added a thrid function which call the GC by and finalizer are called now interessting, here my C# side (shorted)
class Finalized : CriticalFinalizerObject
{
~Finalized()
{
Console.WriteLine("finalized");
}
}

static public class Class1
{
//little hack to use the host as a DLL (and support the host rename)
[UnmanagedCallersOnly]
static void Init(IntPtr textPTr, Int32 textLenght) //the setup
{
var hostName = Marshal.PtrToStringUTF8(textPTr, textLenght);
var assembly = Assembly.GetExecutingAssembly();
DllImportResolver resolver = (string libraryName, Assembly assembly, DllImportSearchPath? searchPath) => {
if (libraryName == "host")
return NativeLibrary.Load(hostName);
return IntPtr.Zero;
};
NativeLibrary.SetDllImportResolver(assembly, resolver);
}

//main loaded by rust side
[UnmanagedCallersOnly]
public static void Main()
{
var finalized = new Finalized(); //pur C# object with a finalizer

var vec = new Vec3I(); //this an unmanaged struct, from rust, just a small test
vec.x = 1;
vec.y = 2;
vec.z = 3;

vec.Print(); //this is bind to rust
}

[UnmanagedCallersOnly]
public static void Cleanup() //recently added cleanup method
{
GC.Collect(); //need else finalizer is never called !
Console.WriteLine("invoking GC right here");
}
}
class Finalized : CriticalFinalizerObject
{
~Finalized()
{
Console.WriteLine("finalized");
}
}

static public class Class1
{
//little hack to use the host as a DLL (and support the host rename)
[UnmanagedCallersOnly]
static void Init(IntPtr textPTr, Int32 textLenght) //the setup
{
var hostName = Marshal.PtrToStringUTF8(textPTr, textLenght);
var assembly = Assembly.GetExecutingAssembly();
DllImportResolver resolver = (string libraryName, Assembly assembly, DllImportSearchPath? searchPath) => {
if (libraryName == "host")
return NativeLibrary.Load(hostName);
return IntPtr.Zero;
};
NativeLibrary.SetDllImportResolver(assembly, resolver);
}

//main loaded by rust side
[UnmanagedCallersOnly]
public static void Main()
{
var finalized = new Finalized(); //pur C# object with a finalizer

var vec = new Vec3I(); //this an unmanaged struct, from rust, just a small test
vec.x = 1;
vec.y = 2;
vec.z = 3;

vec.Print(); //this is bind to rust
}

[UnmanagedCallersOnly]
public static void Cleanup() //recently added cleanup method
{
GC.Collect(); //need else finalizer is never called !
Console.WriteLine("invoking GC right here");
}
}
so the execepted output should look like that:
//print of the vec
finalized (when Gc.collect() is called)
invoking GC right here
//print of the vec
finalized (when Gc.collect() is called)
invoking GC right here
but it actually looks like that
Delta 2.1
Delta 2.15mo ago
No description
Delta 2.1
Delta 2.15mo ago
ok the third parameter of collect handle that GC.Collect(3, GCCollectionMode.Default, true);
Unknown User
Unknown User5mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server
More Posts