none
COMException (ThrowExceptionForHR) => handle is invalid when calling Marshal.FreeHGlobal

    Question

  • Hello

    In my application, i read and write a lot from pointers (System.IntPtr). Somewhere at a "Marshal.FreeHGlobal()" (i've got a lot of them, and it varies where the program stops) i get this Exception:

    "System.Runtime.InteropServices.COMException (0x80070006): The handle is invalid.
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode, IntPtr errorInfo)
       at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
       at System.Runtime.InteropServices.Marshal.FreeHGlobal(IntPtr hglobal)
       at BTR2_DLL_Test.Form1.Button11_Click(Object sender, EventArgs e) in C:\DEV_OLD\VS2003\Testjes\BTR2_DLL_Test\BTR2_DLL_Test\Form1.vb:line 714"

    A closer look gives me the TargetSite: ThrowExceptionForHR

    Has anyone had a similar problem before? Should i provide more information?

    Let me know.

    Kind regards,
    John

    Wednesday, September 06, 2006 4:19 PM

Answers

  • Assuming your FreeHGlobal() properly matches an AllocHGlobal() and you don't call it more than once: you've probably got stray pointers or are overrunning buffers and destroying the heap...
    Friday, September 08, 2006 9:11 PM

All replies

  • Assuming your FreeHGlobal() properly matches an AllocHGlobal() and you don't call it more than once: you've probably got stray pointers or are overrunning buffers and destroying the heap...
    Friday, September 08, 2006 9:11 PM
  • Problem solved. I indeed tried to free the pointer a second time in some cases.

    Thx

    Monday, September 18, 2006 7:00 AM