fixed memory leaks caused by memory owned by vars

issue #1160