SetIlFunctionBody. How to free memory of old body  
Author Message
Sergey Kanzhelev





PostPosted: Building Development and Diagnostic Tools for .Net, SetIlFunctionBody. How to free memory of old body Top

Hi,

Do I have to free the memory, where the method body have been located after SetIlFunctionBody

I have the Profiler that rewrites the body of some methods. Earlier I've freed the old method bodies' memory using code:
.NET Development31  

 
 
David Broman - MSFT





PostPosted: Building Development and Diagnostic Tools for .Net, SetIlFunctionBody. How to free memory of old body Top

Hi, Sergey!

You do not need to (and should not attempt to) free the memory you get back from a call to GetIlFunctionBody. This memory can be mapped directly to the module containing the function and is therefore unfreeable.

The general rule of thumb regarding freeing memory is this. If you allocated the memory, you can free it (unless the API docs say not to, such as with IMethodMalloc::Alloc). If the CLR allocates memory and hands it back to you, don't free it (unless the Profiling API docs say you should). In fact, off the top of my head, I can't think of an example where the profiling API hands you memory that you're supposed to free.