Fix memory leak and compiler warning