About Monkey 2 › Forums › Monkey 2 Development › OnFinalize
This topic contains 3 replies, has 2 voices, and was last updated by
abakobo
1 year, 2 months ago.
-
AuthorPosts
-
January 23, 2018 at 2:15 pm #13176
I remember reading something about a OnFinalize virtual method
Is it functional today? Is it for any kind of classes?
I have some fields to kill manually for now (externals). If there were a finalizer I could call the destructions in the finalizer and that would be neat!
January 23, 2018 at 10:04 pm #13190If you extend the Resource class, you can implement an OnFinalize (and OnDiscard) method. OnFinalize will only be called if OnDiscard hasn’t.
BUT, it should not read/write any monkey2 objects or arrays – just native handles, pointers etc. It should not allocate any GC memory (GC is currently suspended while finalizers run, but future versions may just cause a runtime error if you try to allocate GC memory inside a finalizer).
Monkey2 does not as yet (and may never) attempt to ‘sort’ finalizers by dependancy (it’s impossible with cyclic dependancies anyway, which leads to zombie objects etc – ugly, ugly stuff that can cause incredibly intermitent and hard to find problems) so any object object/array fields may or may not be dead/deallocated etc. I actually attempted a system where it ‘nulled out’ any object/array fields before calling OnFinalize, but there were issues…
So it’s pretty primitive, but it’s enough for fclosing files, freeing malloced memory, deleting gl objects, free type fonts etc, stuff that can be very hard to ‘keep alive’ sanely via ref counting etc.
January 23, 2018 at 10:24 pm #13191Thanks, will check if the class is compatible with a resource extend!
February 5, 2018 at 4:04 pm #13483Looks like I’ll be able to use OnDiscard() Only (i’m storing native classes refs in an array).
But i’m a bit confused with the fact that OnDiscard() might not be called! In which situation can this happen?
If I understand, in normal situations the GC is supposed to call OnDiscard() when cleaning and seeing the Resource object is now out of scope. So OnFinalize() is called on abnormal situations only?
I have mx2 class containing several native classes (OS agnostic ones). So I just want each native one to be deleted when the GC takes care of my Class instance.
Is it OK to create a Resource that has no OnFinalize actions? Or should I use a native data structure to store my native classes refs (instead of an mx2 array)?My aim is that the module end-user won’t have to take care of that, in order to keep the monkeyish feeling of a memoryleaks-safe zone.
thx (sorry there is a lot of question marks in this post..)
-
AuthorPosts
You must be logged in to reply to this topic.