[Mono-osx] CoreMidi Design
Peter Allen Webb
peterallenwebb at gmail.com
Mon Jan 31 16:28:57 EST 2011
I'm working on bindings for the CoreMIDI framework and I'm struggling to
find the best design for wrapping the MidiPacketList and MidiPacket structs
with managed objects.
I want to implement the INativeObject and IDisposable interfaces on my
wrappers for consistency and to support passing the wrapped objects to
Objective-C code, but these structs are not reference-counted like many of
the other things wrapped with INativeObject in MonoMac.
One of my primary concerns is the following scenario:
In response to a MIDI device event, a managed callback in the
MonoMac.CoreMidi code will be called from unmanaged code with a pointer to a
MidiPacketList struct as a parameter. That callback will raise an event for
consumption by client code (i.e. code written by developers using MonoMac
and not wanting to worry about memory allocation issues). I want to include
the managed-code-wrapped MidiPacketList as a property of the event. But if I
just wrapped the native object without copying it, it will probably be freed
after the event has fired, leaving any references the client code might have
to the MidiPacketList with a pointer to freed memory.
These are the solutions I'm considering:
1. Allocate some unmanaged memory with AllocHGlobal, copy the packet list
there, and then set the INativeObject.Handle to point to the copy rather
than the original. Clean up the memory later when Dispose is called.
2. Just don't worry about maintaining native objects in this case at all.
Decode the MidiPacketList to managed objects and move on with life.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Mono-osx