I have a .NET Core C# class that wraps an unmanaged pointer and it should be freed on program exit along with other resource cleanup. However, the destructor is not being called. I have tried in both Debug and Release mode. I see that .NET Core apparently doesn't guarantee that destructors will be run, so what is a recommended workaround? IMO the main point of garbage collection is to avoid having the developer track references, so I find this behavior surprising, to say the least.
From MSDN: In .NET Framework applications (but not in .NET Core applications), finalizers are also called when the program exits.
public Demo { 
  IntPtr _ptr;
  public Demo() 
  { 
    Console.WriteLine("Constructor");
    _ptr = /* P-invoke external function */ 
  ~Demo 
  {
    Console.WriteLine("Destructor");
    /*P-invoke ptr deletion */
  }
}
public static void Main() 
{ 
  Demo demo = new Demo();
  demo = null;
  GC.Collect();
}
Program output:
Constructor
<...>\Test.exe (process 7968) exited with code 0.