Is there any way to destroy a ref manually? My object has
proc `destroy=`(self: Figure) =
gui.eraseFigure(self.handler)
So when object is garbage collected, it is erased on screen as well. However, object is not collected immediately, so I still see it on screen after removing all references to it. Only when GC comes to the object and destroys it, the object disappears, but it may take significant amount of time.
Is there any possibility to say "hey, GC, take a look at this object - it has no references anymore, please collect it asap"?
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
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
Thank you! You described my problem in more general and more meaningful way! I'm not familiar with C# but I liked the Disposable idea. Now I'll follow your advice and will separate "physical" (=destroy()) and "logical" (dispose()) destruction of object.
I love nim community :)