[LLVMdev] LLVM and "who does delete"

John McCall rjmccall at apple.com
Tue Oct 5 14:14:13 PDT 2010


On Oct 5, 2010, at 11:54 AM, Nabil Stendardo wrote:
> I am new to LLVM, and I am pretty much paranoid about loose pointers. I 
> would like to write a compiler in C++ + boost + llvm, but am worried 
> about the pointers created and passed as parameters, especially about 
> whose responsibility it is of deallocating them (does the LLVM API do it 
> internally, or is it the application programmer's responsibility?). And 
> what is the best practice for dealing with pointers (only new, new + 
> delete, encapsulated reference counting just as boost::shared_ptr, etc.)?

Most IR objects have their lifetime automatically managed, either because
they belong to the LLVMContext (like constants and metadata) or because
they are logically parented by another IR object to which they belong (e.g.
Instructions belong to a BasicBlock, which belongs to a Function, which
belongs to a Module).  So the only parts of the LLVM ABI you need to
manually manage are (1) the LLVMContext, (2) the Module, and
(3) normally-parented IR objects which have been removed from their parent
(or never added to one).  (1) and (2) can be easily managed with smart
pointers.  (3) is trickier because you have to ensure that there aren't any
extant dependencies on the object you want to delete, so usually you can't
just hand the work off to some smart pointer.

John.



More information about the llvm-dev mailing list