问题
I have a small language parser that uses LLVM to generate and run code, based on the Kaleidoscope tutorial.
My constraint is that I have to recreate the module because I need to process the same input file multiple times, and after the first compilation, LLVM would complain about the module already containing functions, x, etc. The only way forward I see is to recreate the module in between, and this means that I have to recreate ExecutionEngine
as well.
However, there are a huge amount of memory leaks occurring which I want to get rid of. How do I properly deallocate the llvm::Module
and llvm::ExecutionEngine
?
If I simply delete both, it segfaults somewhere in LLVM. My current implementation goes like this:
TheExecutionEngine->removeModule(module);
delete module();
delete TheExecutionEngine;
However, there are still about 14000 leaks reported, so this seems wrong.
I do not need to duplicate any existing functions in a module. I just want a fresh, empty one.
回答1:
If I understood you correctly, you want to create ExecutionEngine
and Module
instances multiple times during the lifetime of your program, and clean them up properly.
Do not delete the Module separately, since ExecutionEngine
"owns" it once you addModule
. This means it will take care of deleting and cleaning up.
In LLVM it's always best to learn from examples. I suggest you take a look at the source code of tools/lli/lli.cpp
, which does all of what you need, including cleanup.
来源:https://stackoverflow.com/questions/23891889/how-to-deallocate-llvmmodule