r304740 - Fix memory leak exposed by r304726.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 5 15:05:31 PDT 2017


Author: rsmith
Date: Mon Jun  5 17:05:31 2017
New Revision: 304740

URL: http://llvm.org/viewvc/llvm-project?rev=304740&view=rev
Log:
Fix memory leak exposed by r304726.

When giving a ContentCache a null buffer, ignore the DoNotFree flag rather than
inheriting it onto whatever buffer we end up using for the file. Also ensure
that the main buffer is properly destroyed.

Modified:
    cfe/trunk/lib/Basic/SourceManager.cpp

Modified: cfe/trunk/lib/Basic/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceManager.cpp?rev=304740&r1=304739&r2=304740&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/SourceManager.cpp (original)
+++ cfe/trunk/lib/Basic/SourceManager.cpp Mon Jun  5 17:05:31 2017
@@ -73,11 +73,11 @@ void ContentCache::replaceBuffer(llvm::M
     Buffer.setInt(DoNotFree? DoNotFreeFlag : 0);
     return;
   }
-  
+
   if (shouldFreeBuffer())
     delete Buffer.getPointer();
   Buffer.setPointer(B);
-  Buffer.setInt(DoNotFree? DoNotFreeFlag : 0);
+  Buffer.setInt((B && DoNotFree) ? DoNotFreeFlag : 0);
 }
 
 llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag,
@@ -362,9 +362,11 @@ void SourceManager::initializeForReplay(
   // Set up our main file ID as a copy of the old source manager's main file.
   const SLocEntry &OldMainFile = Old.getSLocEntry(Old.getMainFileID());
   assert(OldMainFile.isFile() && "main file is macro expansion?");
-  setMainFileID(createFileID(
-      CloneContentCache(OldMainFile.getFile().getContentCache()),
-      SourceLocation(), OldMainFile.getFile().getFileCharacteristic(), 0, 0));
+  auto *MainCC = CloneContentCache(OldMainFile.getFile().getContentCache());
+  MemBufferInfos.push_back(MainCC);
+  setMainFileID(createFileID(MainCC, SourceLocation(),
+                             OldMainFile.getFile().getFileCharacteristic(),
+                             0, 0));
 
   // Ensure all SLocEntries are loaded from the external source.
   for (unsigned I = 0, N = Old.LoadedSLocEntryTable.size(); I != N; ++I)




More information about the cfe-commits mailing list