[PATCH] D105286: Fix memory leaks

Boleyn Su via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 1 07:48:09 PDT 2021


boleyn.su created this revision.
boleyn.su added a reviewer: chandlerc.
Herald added a subscriber: dexonsmith.
boleyn.su requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

For vector<shared_ptr/uniqeu_ptr>'s, emplace_back(new T()) is not
safe and can lead to memory leak. This happens when emplace_back
fails to allocate required memory and throws, the object allocated
with new will never be deleted. Although this is not an issue
for LLVM as a compiler, but it can hurt those who use it as 
a library.


https://reviews.llvm.org/D105286

Files:
  llvm/include/llvm/IR/PassManager.h


Index: llvm/include/llvm/IR/PassManager.h
===================================================================
--- llvm/include/llvm/IR/PassManager.h
+++ llvm/include/llvm/IR/PassManager.h
@@ -544,7 +544,7 @@
         detail::PassModel<IRUnitT, PassT, PreservedAnalyses, AnalysisManagerT,
                           ExtraArgTs...>;
 
-    Passes.emplace_back(new PassModelT(std::move(Pass)));
+    Passes.emplace_back(make_shared<PassModelT>(std::move(Pass)));
   }
 
   /// When adding a pass manager pass that has the same type as this pass


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105286.355879.patch
Type: text/x-patch
Size: 542 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210701/989258c4/attachment.bin>


More information about the llvm-commits mailing list