cap5lut
cap5lut
CC#
Created by Faker on 3/23/2025 in #help
✅ Dispose method to "dispose" file resource when using LogTo method
if u feel like ur questions are answered, dont forget to mark the thread as that by $close ing it
19 replies
CC#
Created by Faker on 3/23/2025 in #help
✅ Dispose method to "dispose" file resource when using LogTo method
thats an article u want to read if u want to gracefully free resources
19 replies
CC#
Created by Faker on 3/23/2025 in #help
✅ Dispose method to "dispose" file resource when using LogTo method
19 replies
CC#
Created by Faker on 3/23/2025 in #help
✅ 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
CC#
Created by Faker on 3/23/2025 in #help
✅ 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:
public class Example : IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// clean up managed resources (if any)
}
// clean up unmanaged resources (if any)
}
}
public class Example : IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
// clean up managed resources (if any)
}
// clean up unmanaged resources (if any)
}
}
and a derived class with a finalizer would be
public sealed class UnmanagedResourcesExample : Example
{
private IntPtr _unmanagedMemory;
public UnmanagedResourcesExample()
{
_unmanagedMemory = NativeMemory.Alloc(512);
}

protected override Dispose(bool disposing)
{
// doesnt have have managed resources that need disposing so no if (disposing) here
NativeMemory.Free(_unmanagedMemory);
base.Dispose(disposing);
}

~UnmanagedResourcesExample()
{
Dispose(false);
}
}
public sealed class UnmanagedResourcesExample : Example
{
private IntPtr _unmanagedMemory;
public UnmanagedResourcesExample()
{
_unmanagedMemory = NativeMemory.Alloc(512);
}

protected override Dispose(bool disposing)
{
// doesnt have have managed resources that need disposing so no if (disposing) here
NativeMemory.Free(_unmanagedMemory);
base.Dispose(disposing);
}

~UnmanagedResourcesExample()
{
Dispose(false);
}
}
19 replies
CC#
Created by Faker on 3/23/2025 in #help
✅ 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
CC#
Created by Faker on 3/23/2025 in #help
✅ Dispose method to "dispose" file resource when using LogTo method
yes only as last resort fallback, there is no guarantee that finalizers are actually executed. (sorry forgot to mention that)
19 replies
CC#
Created by Faker on 3/23/2025 in #help
✅ Dispose method to "dispose" file resource when using LogTo method
definitively, almost everything uses MS.E.Logging as logging facade (often combined with serilog as logging backend for structured logging)
19 replies
CC#
Created by Faker on 3/23/2025 in #help
✅ 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
CC#
Created by Faker on 3/23/2025 in #help
✅ 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 code
19 replies
CC#
Created by Faker on 3/23/2025 in #help
✅ 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
CC#
Created by Faker on 3/23/2025 in #help
✅ 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
CC#
Created by Yuji on 3/7/2025 in #help
✅ Memory leak or What?
there is the /close slash command
114 replies
CC#
Created by Yuji on 3/7/2025 in #help
✅ Memory leak or What?
well from the provided code u clean up everything as expected, there is nothing left to do there ;p
114 replies
CC#
Created by Yuji on 3/7/2025 in #help
✅ Memory leak or What?
who ever thought of that difference in behaviour is probably a worse drunkard than me 😂
114 replies
CC#
Created by Yuji on 3/7/2025 in #help
✅ Memory leak or What?
oh so it is indeed the one, it just depends on how u create v1 in first place xD
114 replies
CC#
Created by Yuji on 3/7/2025 in #help
✅ Memory leak or What?
anyway i only brought that up to demonstrate that cpp can have sneaky allocations behind the scenes as well 😂
114 replies
CC#
Created by Yuji on 3/7/2025 in #help
✅ Memory leak or What?
then it was another collection type that had that weird quirk
114 replies
CC#
Created by Yuji on 3/7/2025 in #help
✅ Memory leak or What?
kk
114 replies
CC#
Created by Yuji on 3/7/2025 in #help
✅ Memory leak or What?
basically
v1[0}++;
v1[0] == v2[0]
v1[0}++;
v1[0] == v2[0]
would that yield true or false?
114 replies