Fixed memory leaks in algorithm training.