[clang-tidy] Fix a heap use-after-free bug detected by asan.