[llvm] b00fc5a - Fix Mem leak in LLVMTargetMachine.cpp

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 12 02:27:18 PDT 2023


Author: Wang, Xin10
Date: 2023-04-12T05:27:04-04:00
New Revision: b00fc5ac995b982909e685439f3d5cef86b4bed6

URL: https://github.com/llvm/llvm-project/commit/b00fc5ac995b982909e685439f3d5cef86b4bed6
DIFF: https://github.com/llvm/llvm-project/commit/b00fc5ac995b982909e685439f3d5cef86b4bed6.diff

LOG: 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

Added: 
    

Modified: 
    llvm/lib/CodeGen/LLVMTargetMachine.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/LLVMTargetMachine.cpp b/llvm/lib/CodeGen/LLVMTargetMachine.cpp
index 3192dcadb5f5e..d02ec1db1165d 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<MCCodeEmitter> MCE(
+      getTarget().createMCCodeEmitter(*getMCInstrInfo(), *Ctx));
+  std::unique_ptr<MCAsmBackend> MAB(
+      getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions));
   if (!MCE || !MAB)
     return true;
 
   const Triple &T = getTargetTriple();
   std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
-      T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
-      std::unique_ptr<MCCodeEmitter>(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));
 


        


More information about the llvm-commits mailing list