Fix memory leaks and compiler warnings.