fix memory leaks caused by smart pointer cycles

Fixes #940.  The sample input file on issue #940 went from using 92 Mb to 16 Mb!
oops.