py, vm: Free heap-allocated state if it was allocated on the heap.