[LLVMdev] Memory management and LLVM types

Chris Lattner sabre at nondot.org
Tue Nov 1 16:13:55 PST 2005

On Tue, 1 Nov 2005, Evan Jones wrote:

> I am somewhat confused about how I should go about releasing LLVM types. Some 
> types are owned by a Module, and others I am not sure about.

Actually, no types are owned by a module: all types are global entities.

> Are there any general rules or guidelines that I should be aware of? It 
> appears to me that whenever I am passing a "new" object into a container 
> type like a module or basic block that the container owns the object. Is 
> that accurate?

This is true for program objects like instructions and basic blocks. 
Types and constants are special, in that they are automatically "uniqued" 
by the system so that there is only one instance of a particular type 
structure running around in the system.  This allows us to use pointer 
equality to check to see if two types are equivalent.

> If this is true, why does it work for me to:
> - Load a module from bytecode.
> - Get some pointers to types inside that module.
> - Delete the module.
> - Use these types again.

The types are not owned by the module.

> I'm using electric fence, so I don't *think* that it is luck, but I could be 
> wrong. Should I be deleting the pointers that I get out of the module?

Not for types :)

> A related question is how should I delete the module? I see that there is a 
> "dropAllReferences" method. Is this called automatically for me from the 
> Module's destructor?

Yes, it is.  Just deleting the module should work.

If you REALLY want to delve into type archana, hold your breath and read 
some of this:




More information about the llvm-dev mailing list