Fix serveral memory leaks.