Fix leak of LookupCache

The lookup cache isn't allocated in the ASTContext, so seting up a
destructor cleanup isn't sufficient to get the memory released.  Luckily
SourceFile and BuiltinUnit already have their own destructor called, so
we can use std::unique_ptr.

rdar://problem/22387897

Swift SVN r31561
This commit is contained in:
Ben Langmuir
2015-08-28 21:38:05 +00:00
parent 8d656dec8b
commit 54f6bb41ff
2 changed files with 8 additions and 9 deletions

View File

@@ -33,6 +33,7 @@
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
@@ -60,10 +61,8 @@ public:
BuiltinUnit::LookupCache &BuiltinUnit::getCache() const {
// FIXME: This leaks. Sticking this into ASTContext isn't enough because then
// the DenseMap will leak.
if (!Cache) {
const_cast<BuiltinUnit *>(this)->Cache = new LookupCache();
getASTContext().addDestructorCleanup(*Cache);
}
if (!Cache)
const_cast<BuiltinUnit *>(this)->Cache = llvm::make_unique<LookupCache>();
return *Cache;
}
@@ -165,8 +164,8 @@ using SourceLookupCache = SourceFile::LookupCache;
SourceLookupCache &SourceFile::getCache() const {
if (!Cache) {
const_cast<SourceFile *>(this)->Cache = new SourceLookupCache(*this);
getASTContext().addDestructorCleanup(*Cache);
const_cast<SourceFile *>(this)->Cache =
llvm::make_unique<SourceLookupCache>(*this);
}
return *Cache;
}
@@ -1442,7 +1441,7 @@ void SourceFile::clearLookupCache() {
// Abandon any current cache. We'll rebuild it on demand.
Cache->invalidate();
Cache = nullptr;
Cache.reset();
}
void