[LTO API] fix memory leakage introduced at r230290.

r230290 released the LLVM module but not the LTOModule.

rdar://19024554


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230544 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Manman Ren
2015-02-25 21:20:53 +00:00
parent 92d1637e2f
commit f2a351a514
2 changed files with 17 additions and 4 deletions

View File

@ -155,6 +155,7 @@ private:
typedef StringMap<uint8_t> StringSet; typedef StringMap<uint8_t> StringSet;
void initialize(); void initialize();
void destroyMergedModule();
std::unique_ptr<LLVMContext> OwnedContext; std::unique_ptr<LLVMContext> OwnedContext;
LLVMContext &Context; LLVMContext &Context;
Linker IRLinker; Linker IRLinker;
@ -172,6 +173,7 @@ private:
TargetOptions Options; TargetOptions Options;
lto_diagnostic_handler_t DiagHandler; lto_diagnostic_handler_t DiagHandler;
void *DiagContext; void *DiagContext;
LTOModule *OwnedModule;
}; };
} }
#endif #endif

View File

@ -82,16 +82,27 @@ void LTOCodeGenerator::initialize() {
CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT; CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT;
DiagHandler = nullptr; DiagHandler = nullptr;
DiagContext = nullptr; DiagContext = nullptr;
OwnedModule = nullptr;
initializeLTOPasses(); initializeLTOPasses();
} }
void LTOCodeGenerator::destroyMergedModule() {
if (OwnedModule) {
assert(IRLinker.getModule() == &OwnedModule->getModule() &&
"The linker's module should be the same as the owned module");
delete OwnedModule;
OwnedModule = nullptr;
} else if (IRLinker.getModule())
IRLinker.deleteModule();
}
LTOCodeGenerator::~LTOCodeGenerator() { LTOCodeGenerator::~LTOCodeGenerator() {
destroyMergedModule();
delete TargetMach; delete TargetMach;
TargetMach = nullptr; TargetMach = nullptr;
IRLinker.deleteModule();
for (std::vector<char *>::iterator I = CodegenOptions.begin(), for (std::vector<char *>::iterator I = CodegenOptions.begin(),
E = CodegenOptions.end(); E = CodegenOptions.end();
I != E; ++I) I != E; ++I)
@ -146,10 +157,10 @@ void LTOCodeGenerator::setModule(LTOModule *Mod) {
"Expected module in same context"); "Expected module in same context");
// Delete the old merged module. // Delete the old merged module.
if (IRLinker.getModule()) destroyMergedModule();
IRLinker.deleteModule();
AsmUndefinedRefs.clear(); AsmUndefinedRefs.clear();
OwnedModule = Mod;
IRLinker.setModule(&Mod->getModule()); IRLinker.setModule(&Mod->getModule());
const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs(); const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();