Fix some problems with memory leaks

Authored by sirgienko on Mar 14 2019, 10:27 PM.