I'm working in Flash, and attempting to use the new "domain memory" available in Flash Player. This essentially lets you work with memory at a low level, but you have to manage the memory yourself, much like C++, which has no built-in garbage collector. I've built a basic allocator/deallocator, but I need some way to build a Garbage Collector or Reference Counter so I can unallocate unused objects. Take the following example:
Rect stageRect = new Rect(0, 0, stage.width, stage.height);
// syntax is for understanding only
// actually would allocate memory using my handwritten allocator
I've constructed a new Rect and stored in a class member var. Now lets say I perform some rectangle math on this object, creating 2 more objects.
Rect quarterRect = stageRect.halfWidth().halfHeight();
As you can see, the Rect returned by halfWidth is unused, and can be garbage collected.
The final rect created by halfHeight is stored in the var quarterRect, which I need for later.
How do I detect such unused objects, and dispose of them accordingly? I've been reading up on Reference Counting, Smart Pointers, the GC for C++, but I still can't figure out how to detect when a reference is unused, to decrement the reference count. Incrementing the ref count is easy : when you set another var to point to this object, ie: a = stageRect, should increase the reference count of stageRect, but how would you know when a is unused? to decrement the reference count? Usually you don't go around setting a = null in modern code. You just expect the platform to detect its an unused ref and dispose it.
Rectreturned byhalfWidthis unused? - Robin Rodricksa=nullis exactly what needs to be done. This is true for the built-in GC too. TBH I doubt it's worth the trouble to write your own GC, except if manually freeing up memory is mandatory for your application. - Creynders