Search
Get Started
C
C#
•
17mo ago
DevLop games
Destructor not being called
I am testing a few things and am having trouble getting the destructor to work
. It
's supposed to print some text when called
. Instead nothing happens
. Anyone know why
?
C#
Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,010
Members
View on Discord
Was this page helpful?
Yes
No
J
jcotton42
•
8/18/24, 6:14 AM
Finalizers are called by the garbage collector
@DevLop games
, and there is no guarantee when they
'll be called
.
J
jcotton42
•
8/18/24, 6:15 AM
In general there are very very few cases where you need a finalizer
.
And pretty much all of them involve unmanaged resources
.
J
jcotton42
•
8/18/24, 6:16 AM
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers
Finalizers - C#
Finalizers in C
# perform any necessary final clean
-up when a class instance is being collected by the garbage collector
.
J
jcotton42
•
8/18/24, 6:16 AM
given your use of
"destructor
" do you by chance have a background in C
+
+
?
@DevLop games
J
jcotton42
given your use of "destructor" do you by chance have a background in C++? @DevLo...
D
DevLop games
OP
•
8/18/24, 7:39 AM
I
've used it before
D
DevLop games
I've used it before
J
jcotton42
•
8/18/24, 7:40 AM
C
# does not have RAII like C
+
+ does
. Finalizers are not fired deterministically
.
J
jcotton42
•
8/18/24, 7:40 AM
If you need deterministic destruction of something in C
# use
https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-idisposable
System.IDisposable interface - .NET
Learn about the System
.IDisposable interface
.
D
DevLop games
OP
•
8/18/24, 7:54 AM
Any simple way of doing it
?
J
jcotton42
•
8/18/24, 7:54 AM
doing what
?
D
DevLop games
OP
•
8/18/24, 7:56 AM
Deleting resources when the project is closed or ended
J
jcotton42
•
8/18/24, 7:59 AM
@DevLop games
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using
using statement - ensure the correct use of disposable objects - C#...
Use the C
# using statement or declaration to ensure the correct use of disposable objects