From b00fc5ac995b982909e685439f3d5cef86b4bed6 Mon Sep 17 00:00:00 2001 From: "Wang, Xin10" Date: Wed, 12 Apr 2023 05:26:00 -0400 Subject: [PATCH] Fix Mem leak in LLVMTargetMachine.cpp If we go to line 302, with one of MCE or MAB is not nullptr, then we could leak mem here. Use unique_ptr to maintain these 2 pointer can avoid it. Reviewed By: LuoYuanke Differential Revision: https://reviews.llvm.org/D148003 --- llvm/lib/CodeGen/LLVMTargetMachine.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/llvm/lib/CodeGen/LLVMTargetMachine.cpp b/llvm/lib/CodeGen/LLVMTargetMachine.cpp index 3192dcad..d02ec1d 100644 --- a/llvm/lib/CodeGen/LLVMTargetMachine.cpp +++ b/llvm/lib/CodeGen/LLVMTargetMachine.cpp @@ -274,16 +274,17 @@ bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, // emission fails. const MCSubtargetInfo &STI = *getMCSubtargetInfo(); const MCRegisterInfo &MRI = *getMCRegisterInfo(); - MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getMCInstrInfo(), *Ctx); - MCAsmBackend *MAB = - getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); + std::unique_ptr MCE( + getTarget().createMCCodeEmitter(*getMCInstrInfo(), *Ctx)); + std::unique_ptr MAB( + getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions)); if (!MCE || !MAB) return true; const Triple &T = getTargetTriple(); std::unique_ptr AsmStreamer(getTarget().createMCObjectStreamer( - T, *Ctx, std::unique_ptr(MAB), MAB->createObjectWriter(Out), - std::unique_ptr(MCE), STI, Options.MCOptions.MCRelaxAll, + T, *Ctx, std::move(MAB), MAB->createObjectWriter(Out), std::move(MCE), + STI, Options.MCOptions.MCRelaxAll, Options.MCOptions.MCIncrementalLinkerCompatible, /*DWARFMustBeAtTheEnd*/ true)); -- 2.7.4