C
C#14mo ago
Raki

❔ Why deconstuctor/Finalize example not working in vs 2022. Anything missing

using System;

class GCcollection
{
public GCcollection()
{
Console.WriteLine("Hey why are u creating me");
}

~GCcollection()
{
Console.WriteLine("Life is over, BYe");
}
}

class AsUsualMainClass
{
public static void Main(string[] args)
{
DoStuf();
}

public static void DoStuf()
{
var create = new GCcollection();
create = null;
GC.Collect();
Console.ReadKey();
}
}
using System;

class GCcollection
{
public GCcollection()
{
Console.WriteLine("Hey why are u creating me");
}

~GCcollection()
{
Console.WriteLine("Life is over, BYe");
}
}

class AsUsualMainClass
{
public static void Main(string[] args)
{
DoStuf();
}

public static void DoStuf()
{
var create = new GCcollection();
create = null;
GC.Collect();
Console.ReadKey();
}
}
7 Replies
Raki
Raki14mo ago
I'm just pressing F5 to run this but Ideally the deconstructor should be called when GC.Collect executes but in my console window. I don't see it
cap5lut
cap5lut14mo ago
generally speaking you should not rely on finalizers, but use the dispose pattern instead https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose
Implement a Dispose method
In this article, learn to implement the Dispose method, which releases unmanaged resources used by your code in .NET.
Florian Voß
Florian Voß14mo ago
@ramji it seems you just have to move the garbage collection outside of its scope, and add a little delay to give the finalizer thread enough time to finish it's work:
class AsUsualMainClass
{
public static void Main(string[] args)
{
DoStuf();
GC.Collect();
Thread.Sleep(1000);
}

public static void DoStuf()
{
var create = new GCcollection();
create = null;
}
}
class AsUsualMainClass
{
public static void Main(string[] args)
{
DoStuf();
GC.Collect();
Thread.Sleep(1000);
}

public static void DoStuf()
{
var create = new GCcollection();
create = null;
}
}
this works as expected there is also GC.WaitForPendingFinalizers() to make it not as hardcoded
Chiyoko_S
Chiyoko_S14mo ago
Finalizers are not run immediately after they are collected if the GC finds an object that has a finalizer & is not referenced anywhere, the object with the finalizer is put on a finalization queue to have its resource cleaned up then unallocated... eventually there is no guarantees on when the finalizer will run which is why you should avoid relying solely on finalizers, and use Dispose() patterns instead; the resource clean-ups are deterministic this way (because you do the clean-up yourself) also rarely is there a need to write finalizers yourself. You should never write your own unless you are doing the unmanaged resource management yourself
JakenVeina
JakenVeina14mo ago
Finalizers are not guaranteed to run, ever, no matter what you do
JakenVeina
JakenVeina14mo ago
ericlippert
Fabulous adventures in coding
When everything you know is wrong, part one
Finalizers are interesting and dangerous because they are an environment in which everything you know is wrong. I’ve written a lot about the perils of C# finalizers / destructors (either name…
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Blazor Project won't compile - Previously did without any changes.Hi, I have a blazor project, which previously compiled and ran without issue. My last commit was sub❔ How can I import json file inside a sass file?I have looked around in the web for various solutions and found it could be done easily in javascrip❔ .NET Core 7.0 SDK shown as installed by "apt-get" but not used by "dotnet" commandI am using dotnet-cli and I have been facing this issue with my dotnet installation on Ubuntu Jammy ❔ EF Core Fluent API - Unidirectional one to many relationshipHow can I implement a one to many unidirectional relationship using EF Core fluent API? (Image 1) I❔ Is it a problem to be creating lots of empty lists etc just to avoid null checks?Over thousands of items, is this expensive or a problem? What is a better way to avoid null checks? ❔ Decent terminal UI library or solutionI'm writing a terminal-based application that requires a TUI. I've tried Terminal.Gui but I've found❔ Unable to bind CollectionView to DictionaryWhen binding a `CollectionView` to a Dictionary, MAUI throws a XAML exception of ``` Error: Cannot ✅ How to make program wait before executing next print line?```cs Console.WriteLine("This is line one"); // sleep 2 seconds Console.WriteLine("This is line two"✅ Navigating my window only display the next page's name instead of its contentsI hope you're all well. I got stuck on a WPF XAML problem where my pages wouldn't navigate and afterMaking an identifier for defined generic typeWhy this does work?