Fix potential memory leaks found in ci scan