Вопрос

First, I allocate memory and get the pointer:

IntPtr pointer = Marshal.AllocHGlobal(size);

Then, I try to release the memory pointed by my pointer:

Marshal.FreeHGlobal(pointer);

But in my application it raises sometimes a fatal unknown exception with just this explaination: "a memory fragment has been damaged". I guess this is because I try to free a pointer that points to a memory block already disallocated by freeing another pointer...

My question is: using the IntPtr pointer, is there any way to check if the memory block is still allocated or has been already disallocated ?

Это было полезно?

Решение

The good way to free your pointer is Marshal.FreeHGlobal(), so if it is disallocated, it's a logic problem, but you could do it in a try/catch, so it's already disallocated if you get into the catch exception.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top