Open PMunch opened 1 year ago
After writing this I discovered deallocHeap
in the module gc_common
, unfortunately this isn't available for ARC/ORC it seems.
Did some testing and exposing deallocOsPages
and calling that in the dynlib_deinit
indeed made the memory consumption stop increasing. This won't call finalizers/destructors though, which isn't great, but at least it points to deallocHeap
as the thing that has to be implemented for ARC/ORC in order to make this possible.
Just for reference if someone wants to give a go at implementing this. I believe that this procedure is what needs to be ported to ARC/ORC and then create a NimExit
which basically just calls it:
Makes sense but this new NimExit
won't run destructors, it simply cannot as the RTTI is not always available.
Hmm, that's a bit unfortunate. But still better than just leaving all the memory dangling. Does deallocHeap
imply GC_FullCollect
? Let's say I have an object which wraps a file and the destructor flushes my changes. If my program ends while that object still exists the changes won't be flushed (poor design, but it's just an example). Since deallocHeap
can't run this destructor (because it can't see which type it is) I would then have to ensure that it is freed before I exit. If I then set my reference to this object to nil and call GC_FullCollect
this destructor should be run, but if I omit the GC_FullCollect
call does deallocHeap
then call this destructor? My guess is no.
deallocHeap
should not call GC_FullCollect
but NimExit
should call it.
deallocHeap
should not callGC_FullCollect
butNimExit
should call it.
is this also addressed by https://github.com/nim-lang/Nim/commit/e217bb24a1e5b92d448bf7afa2488bec89b94cfb ?
Summary
This came about when working with dynamic libraries. If you write a dynamic library in Nim and then load it into a separate program there is no (documented) way to free all the memory if the loader program unloads the dynamic library. This means that if a system loads and unloads a dynamic library it will keep increasing in memory.
Description
What I propose is to create a procedure
NimQuit
opposite ofNimMain
which will destroy all memory. Calling any other code using GC after that will be undefined behaviour. This would then be called automatically in.fini
by attaching__attribute((destructor))__
or hooking inDllMain
, similar to howNimMain
is called today. And analogous to how--noMain
disables this behaviour--noQuit
should prevent it from being called andNimQuit
must be called exlicitly.Alternatives
No response
Examples
As a simple example/testing ground I wrote this:
loader.nim
:library.nim
:Compiled with:
and
Backwards Compatibility
No response
Links
No response