clearing heap on exit correctly