- // Finally, free all our storage
- freeStorage();
-
-#if defined(DEBUG)
- /* and shut down the allocator debugging */
- shutdownAllocator();
-#endif
+ // Finally, free all our storage. However, we only free the heap
+ // memory if we have waited for foreign calls to complete;
+ // otherwise a foreign call in progress may still be referencing
+ // heap memory (e.g. by being passed a ByteArray#).
+ freeStorage(wait_foreign);