Help with Marshal.FreeCoTaskMem

  • Thread starter Thread starter roidy
  • Start date Start date
R

roidy

Hi, I have the following code :-

Dim fhandle as UIntPtr
dim asize as UInteger
fhandle = MP4Read("e:\test.m4v", 0)

Dim buffer As IntPtr = Marshal.AllocCoTaskMem(411706)

MP4GetMetadataCoverArt(fhandle, buffer, asize, 0)

Dim data(411706) As Byte
Marshal.Copy(buffer, data, 0, 411705)
Marshal.FreeCoTaskMem(buffer)

Dim ms As New IO.MemoryStream(data)
PictureBox1.Image = Image.FromStream(ms)

MP4Close(fhandle)


This calls functions from libmp4v2.dll (which is written in c++) to return
metadata from mp4 video files. What it does is allocates 411706 bytes and
passes the pointer (buffer) to the MP4GetMetadataCoverArt function in order
to retrieve the artwork from a video file. A byte array (data) is then
created and the content of buffer is copied into it and then displayed in a
PictureBox.

The problem is when I call Marshal.FreeCoTaskMem I get an
AccessViolationException Attempted to read or write protected memory error.

Now if I remove the Marshal.FreeCoTaskMem line the program works fine and
the art work is displayed in the picturebox.

Any help as to why Marshal.FreeCoTaskMem would give me a exception?

Thanks
Rob
 
Back
Top