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