SetIlFunctionBody. How to free memory of old body
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:
CComPtr<IMalloc> pDealloc = NULL;
CHECK_COM(ecatLocal, CoGetMalloc(1, &pDealloc));
pDealloc->Free(pMethod);
But on the Vista this code is fails down. Maybe freeing of the memory was wrong? I didn't find any menthions of how this memory will be cleared in the blogs.
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.