getStablePtr((StgPtr)runSparks_closure);
getStablePtr((StgPtr)ensureIOManagerIsRunning_closure);
+#ifndef mingw32_HOST_OS
getStablePtr((StgPtr)runHandlers_closure);
+#endif
/* initialise the shared Typeable store */
initGlobalStore();
/* stop the ticker */
stopTimer();
- exitTimer();
+ exitTimer(wait_foreign);
// set the terminal settings back to what they were
#if !defined(mingw32_HOST_OS)
/* free hash table storage */
exitHashTable();
- // Finally, free all our storage
- freeStorage();
+ // 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);
#if defined(DEBUG)
/* and shut down the allocator debugging */