cap5lut
✅ Dispose method to "dispose" file resource when using LogTo method
finalizers are a bit tricky.
because they run directly before the memory is collected/freed, if u mess up in the finalizer, u could happen to "revive" the instance.
an instance is collected when there is no reference to it anymore, so if u eg would add
this
in a finalizer to some other instance thats still alive and it stores that reference, you rooted it again.
then the GC wont collect the instance. but u wasted quite some GC's time because it went through the struggle to determine that it was ready for collection.
(and it would also end up in a higher generation)19 replies
✅ Dispose method to "dispose" file resource when using LogTo method
if ever there are other classes that inherit from this class, we set up a finalizer for them?no, u dont set up a finalizer for them. they might implement a finalizer. the typical dispose pattern for unsealed types is: and a derived class with a finalizer would be
19 replies
✅ Dispose method to "dispose" file resource when using LogTo method
while process memory and opened file handles are automatically returned to the OS/the latter closed on process exit, other unmanaged resources might not.
a typical example is GPU memory, if u upload for example a texture to the GPU, it stays there until u free it.
if ur process exists without doing so, that memory isnt usable until u restart the system.
for such stuff the finalizer exists. "lets pray that we can clean up the mess the user caused"
19 replies
✅ Dispose method to "dispose" file resource when using LogTo method
ef core has for example MS.E.Logging integration: https://learn.microsoft.com/en-us/ef/core/logging-events-diagnostics/
19 replies
✅ Dispose method to "dispose" file resource when using LogTo method
now to the logging part:
logging is a solved problem, use existing solutions like
Microsoft.Extensions.Logging
and/or serilog for logging. these logging frameworks are fleshed out and have decades of experience and improvements.
it works, u have less code to maintain and can worry about ur actual code19 replies
✅ Dispose method to "dispose" file resource when using LogTo method
disposing an object means that you are done with it, its bad practice to "revive" such an object and it adds a lot of complexity.
the better practice here is to simply dispose, and create a new instance.
19 replies
✅ Dispose method to "dispose" file resource when using LogTo method
finalizers are special methods (like the constructor) that might run before the instance is actually destroyed and its memory is "released".
they are a last resort fallback to free unmanaged resources, in case the user of the type forgot to call dispose themselves.
if dispose is called, all resources should be freed already, so there is no need to run that finalizer.
u most likely dont have a finalizer in ur class, but ur class is not sealed, so a derived type could end up having a finalizer.
19 replies