[PATCH] D93249: Frontend: Fix memory leak in CompilerInstance::setVerboseOutputStream

Duncan P. N. Exon Smith via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 14 14:45:31 PST 2020


dexonsmith created this revision.
dexonsmith added a reviewer: erik.pilkington.
Herald added a subscriber: ributzka.
dexonsmith requested review of this revision.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93249

Files:
  clang/lib/Frontend/CompilerInstance.cpp
  clang/unittests/Frontend/OutputStreamTest.cpp


Index: clang/unittests/Frontend/OutputStreamTest.cpp
===================================================================
--- clang/unittests/Frontend/OutputStreamTest.cpp
+++ clang/unittests/Frontend/OutputStreamTest.cpp
@@ -101,4 +101,12 @@
   EXPECT_TRUE(StringRef(VerboseBuffer.data()).contains("errors generated"));
 }
 
+TEST(FrontendOutputTests, TestVerboseOutputStreamOwnedNotLeaked) {
+  CompilerInstance Compiler;
+  Compiler.setVerboseOutputStream(std::make_unique<raw_null_ostream>());
+
+  // Trust leak sanitizer bots to catch a leak here.
+  Compiler.setVerboseOutputStream(llvm::nulls());
+}
+
 } // anonymous namespace
Index: clang/lib/Frontend/CompilerInstance.cpp
===================================================================
--- clang/lib/Frontend/CompilerInstance.cpp
+++ clang/lib/Frontend/CompilerInstance.cpp
@@ -85,7 +85,7 @@
 }
 
 void CompilerInstance::setVerboseOutputStream(raw_ostream &Value) {
-  OwnedVerboseOutputStream.release();
+  OwnedVerboseOutputStream.reset();
   VerboseOutputStream = &Value;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93249.311717.patch
Type: text/x-patch
Size: 1044 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201214/25d9ab5e/attachment.bin>


More information about the cfe-commits mailing list