[virtmach] Exception Safety

thaddaeus.frogley@creaturelabs.com thaddaeus.frogley@creaturelabs.com
Thu, 20 Apr 2000 11:08:04 +0100


When creating a VM which interacts with an object orientated environment,
written in C++, it is desirable to prevent the VM from causing memory leaks.
This could be done by  storing objects created by bytecode in the VMs memory
space (stack), but as there is no ABI for C++, it would be very hard to
determine a) which parts of the VM stack contain C++ objects, and b) how to
destroy them in the case of an exception.

I'm considering the following solution.  In addition to the programs
stack/memory space, the VM provides, as an implementation detail, a
finalisation stack, which contains C++ objects derived from a specific
virtual base class.  These objects are destroyed at the end of a scripts
lifetime, whether or not it completed execution successfully.  It is up to
the opcode implementations to make sure any objects created by the script
are have cleanup mechanisms placed on the finalisation stack.

Can anyone see any problems with this solution, and/or a better solution.

Thad

---
Thaddaeus Frogley
Certified C/C++ Programmer
[http://www.brainbench.com/transcript.jsp?pid=334344]
Beasts Team, 
Creature Labs Ltd
thaddaeus.frogley@creaturelabs.com