Merge memory leak fixes by Glenn Pavlovic