As far as I'm aware, it is considered bad practice to mix the **destruction** 
of an object with the **deallocation** of an object's memory. For example C# 
does separate those two concerns very explicitly by providing its IDisposable 
interface that is exactly intended for such usecases that you seem to have.

see [Dispose 
Pattern](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/dispose-pattern)

I'm not yet totally fluent with nim yet, so I can't give a solid answer on how 
to best implement the **IDisposable** pattern in nim, but I would always 
recommend against letting the GC decide when the **logical** lifetime of an 
object has ended (for all the same reasons explained by . You will very likely 
end up fighting the GC more and more instead of it being the helpful tool that 
it is supposed to be.

I think I would try and emulate the pattern described by Microsoft by just 
providing a proc Dispose(this: XXX) and then also emulate the using syntax of 
C# with some simple meta-programming similar as described in [this blog 
post](https://hookrace.net/blog/introduction-to-metaprogramming-in-nim/#safe-locking)

Reply via email to