fix potential memory leak and delete/delete[] mismatch