Gilad
Gilad

Reputation: 6595

BadImageFormatException disposing

I'm running on X64

here is my code:

 ColorFileMapping = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, _byteCount, null);
 ViewerImageData = MapViewOfFile(ColorFileMapping, 0xF001F, 0, 0, _byteCount);

however when I try to dispose of this IntPtr i get a BadImageFormatException

can you please explain why?

public void Dispose()
 {              
     Marshal.FreeHGlobal(ViewerImageData); //here i get the exception
     Marshal.FreeHGlobal(ColorFileMapping);
 }

Upvotes: 0

Views: 256

Answers (1)

Sean
Sean

Reputation: 62492

It you call to MapViewOfFile is actually a call to the Windows API function MapViewOfFile then you should not treat it as a HGLOBAL. It's pointer to an area of memory, and when you've finished with it you need to call UnmapViewOfFile.

Also, the HANDLE you get back from CreateFileMapping should be closed by calling CloseHandle.

Upvotes: 2

Related Questions