G
Guest
Hi,
I am using a MIDI header structure MIDIHDR. The header contains a pointer
lpData to a string sSysEx, which is filled by an API call back routine using
that pointer.
The structure, an instance mhdr of the header, the string and its handle are
declared on main module level:
<StructLayout(LayoutKind.Sequential)> _
Structure MIDIHDR
Dim lpData as Integer
Dim ...(other integer data)
End Structure
Public mhdr As MIDIHDR
Public sSysEx As String = New String("RRR")
Public GCHSysEx as GCHandle = GCHandle.Alloc(sSysEx, GCHandleType.Pinned)
In a particular sub the header is filled and the midi input is started:
mhdr.lpData = GCHSysEx.AddrOfPinnedObject.ToInt32
mhdr. ...etc...
lRet = midiInStart
and the Midi call back routine fills the string. All OK so far.
However, the sub can only be called once. When it is called again, the
Garbage Collector appears to have moved the string and the pointer is
invalid. As far as I understand, that should not happen as the memory is
pinned and the handle is not yet unpinned by a GCHSysEx.Free statement.
Anyone who knows how to pin memory in a correct way or knows about the
'scope' of a pinned object?
Thanks,
Reinier
I am using a MIDI header structure MIDIHDR. The header contains a pointer
lpData to a string sSysEx, which is filled by an API call back routine using
that pointer.
The structure, an instance mhdr of the header, the string and its handle are
declared on main module level:
<StructLayout(LayoutKind.Sequential)> _
Structure MIDIHDR
Dim lpData as Integer
Dim ...(other integer data)
End Structure
Public mhdr As MIDIHDR
Public sSysEx As String = New String("RRR")
Public GCHSysEx as GCHandle = GCHandle.Alloc(sSysEx, GCHandleType.Pinned)
In a particular sub the header is filled and the midi input is started:
mhdr.lpData = GCHSysEx.AddrOfPinnedObject.ToInt32
mhdr. ...etc...
lRet = midiInStart
and the Midi call back routine fills the string. All OK so far.
However, the sub can only be called once. When it is called again, the
Garbage Collector appears to have moved the string and the pointer is
invalid. As far as I understand, that should not happen as the memory is
pinned and the handle is not yet unpinned by a GCHSysEx.Free statement.
Anyone who knows how to pin memory in a correct way or knows about the
'scope' of a pinned object?
Thanks,
Reinier