[LLVMdev] memory leaks in *Type::get() and Constant*::get()

Nuno Lopes nunoplopes at sapo.pt
Fri Oct 3 11:27:11 PDT 2008


Hi,

Per my previous post about a patch to run the test suite under valgrind 
(btw, can I commit it?), I've tracked a few memory leaks in LLVM. I've fixed 
4 trivial ones, but there are a few more not-so-trivial remaining.

Take a look at two common reports by valgrind:

240 (144 direct, 96 indirect) bytes in 3 blocks are definitely lost in loss 
record 17 of 20
   at 0x4023614: operator new(unsigned) (in vgpreload_memcheck.so)
   by 0x866B4B3: llvm::PointerType::get(llvm::Type const*, unsigned) 
(Type.cpp:1319)
   by 0x85E656E: llvm::BitcodeReader::ParseTypeTable() 
(BitcodeReader.cpp:498)
   by 0x85EDDB1: llvm::BitcodeReader::ParseModule(std::string const&) 
(BitcodeReader.cpp:1078)
   by 0x85EF35F: llvm::BitcodeReader::ParseBitcode() 
(BitcodeReader.cpp:1367)
   by 0x85EF40B: llvm::getBitcodeModuleProvider(llvm::MemoryBuffer*, 
std::string*) (BitcodeReader.cpp:2078)
   by 0x85EF483: llvm::ParseBitcodeFile(llvm::MemoryBuffer*, std::string*) 
(BitcodeReader.cpp:2094)
   by 0x839ADFF: main (opt.cpp:432)


160 bytes in 4 blocks are definitely lost in loss record 18 of 20
   at 0x4023614: operator new(unsigned) (in vgpreload_memcheck.so)
   by 0x867EF30: llvm::User::operator new(unsigned, unsigned) 
(Value.cpp:384)
   by 0x861F3EE: llvm::ConstantInt::operator new(unsigned) (Constants.h:55)
   by 0x860E794: llvm::ConstantInt::get(llvm::APInt const&) 
(Constants.cpp:272)
   by 0x860E831: llvm::ConstantInt::get(llvm::Type const*, unsigned long 
long, bool) (Constants.cpp:254)
   by 0x860ECE2: llvm::Constant::getNullValue(llvm::Type const*) 
(Constants.cpp:109)
   by 0x85E7AB0: llvm::BitcodeReader::ParseConstants() 
(BitcodeReader.cpp:768)
   by 0x85EDE17: llvm::BitcodeReader::ParseModule(std::string const&) 
(BitcodeReader.cpp:1090)
   by 0x85EF35F: llvm::BitcodeReader::ParseBitcode() 
(BitcodeReader.cpp:1367)
   by 0x85EF40B: llvm::getBitcodeModuleProvider(llvm::MemoryBuffer*, 
std::string*) (BitcodeReader.cpp:2078)
   by 0x85EF483: llvm::ParseBitcodeFile(llvm::MemoryBuffer*, std::string*) 
(BitcodeReader.cpp:2094)
   by 0x839ADFF: main (opt.cpp:432)


The problem here is that these methods add newly created types/constants to 
a static variable, which is managed by the ManagedStatic template. This 
template effectively deletes the map, but does not frees the Types*.
I made a quick&dirty patch 
(http://web.ist.utl.pt/nuno.lopes/llvm_types_cleanup.txt), but it doesn't 
seem to fix the problem (it makes llvm crash), since it seems there are some 
statically allocated Types.

Any ideas on how to kill these memleaks?

Nuno 




More information about the llvm-dev mailing list