[llvm] r230544 - [LTO API] fix memory leakage introduced at r230290.

Duncan P. N. Exon Smith dexonsmith at apple.com
Wed Feb 25 13:29:09 PST 2015


> On 2015 Feb 25, at 13:20, Manman Ren <manman.ren at gmail.com> wrote:
> 
> Author: mren
> Date: Wed Feb 25 15:20:53 2015
> New Revision: 230544
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=230544&view=rev
> Log:
> [LTO API] fix memory leakage introduced at r230290.
> 
> r230290 released the LLVM module but not the LTOModule.
> 
> rdar://19024554
> 
> Modified:
>    llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h
>    llvm/trunk/lib/LTO/LTOCodeGenerator.cpp
> 
> Modified: llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h?rev=230544&r1=230543&r2=230544&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h (original)
> +++ llvm/trunk/include/llvm/LTO/LTOCodeGenerator.h Wed Feb 25 15:20:53 2015
> @@ -155,6 +155,7 @@ private:
>   typedef StringMap<uint8_t> StringSet;
> 
>   void initialize();
> +  void destroyMergedModule();
>   std::unique_ptr<LLVMContext> OwnedContext;
>   LLVMContext &Context;
>   Linker IRLinker;
> @@ -172,6 +173,7 @@ private:
>   TargetOptions Options;
>   lto_diagnostic_handler_t DiagHandler;
>   void *DiagContext;
> +  LTOModule *OwnedModule;

Can this just be `unique_ptr<LTOModule>`?

I think that simplifies the rest of the patch.

> };
> }
> #endif
> 
> Modified: llvm/trunk/lib/LTO/LTOCodeGenerator.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOCodeGenerator.cpp?rev=230544&r1=230543&r2=230544&view=diff
> ==============================================================================
> --- llvm/trunk/lib/LTO/LTOCodeGenerator.cpp (original)
> +++ llvm/trunk/lib/LTO/LTOCodeGenerator.cpp Wed Feb 25 15:20:53 2015
> @@ -82,16 +82,27 @@ void LTOCodeGenerator::initialize() {
>   CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT;
>   DiagHandler = nullptr;
>   DiagContext = nullptr;
> +  OwnedModule = nullptr;
> 
>   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() {
> +  destroyMergedModule();
> +
>   delete TargetMach;
>   TargetMach = nullptr;

Maybe `TargetMach` should be changed to a `unique_ptr<>` as
well in a follow-up, not sure.

> 
> -  IRLinker.deleteModule();
> -
>   for (std::vector<char *>::iterator I = CodegenOptions.begin(),
>                                      E = CodegenOptions.end();
>        I != E; ++I)
> @@ -146,10 +157,10 @@ void LTOCodeGenerator::setModule(LTOModu
>          "Expected module in same context");
> 
>   // Delete the old merged module.
> -  if (IRLinker.getModule())
> -    IRLinker.deleteModule();
> +  destroyMergedModule();
>   AsmUndefinedRefs.clear();
> 
> +  OwnedModule = Mod;
>   IRLinker.setModule(&Mod->getModule());
> 
>   const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list