MIDI receive callback context

Up until iOS 14, the signature of MIDI message receive callbacks was the following:

typedef void (*MIDIReadProc)(const MIDIPacketList *pktlist, void *readProcRefCon, void *srcConnRefCon);

where readProcRefCon was an arbitrary pointer that could be used to capture context such as a pointer to a C++ object to call into, or even a function pointer encapsulating that call.

However, this has now been deprecated and replaced with the following:

typedef void (^MIDIReceiveBlock)(const MIDIEventList *evtlist, void *srcConnRefCon);

The readProcRefCon parameter has now been removed, making it far from straightforward to pass messages along to other services without resorting to some sort of global variable, which would be an anti-pattern. So, is there some way to avoid that?

Documentation for the old signature is here and for the new signature is here. No explanation is given as to why the context pointer was removed.

Accepted Reply

No explanation is given as to why the context pointer was removed.

One possible reason may be that the block can capture the context pointer, so you have no need to get it from a parameter of callback function.

  • Good point @OOPer - I didn't notice that this isn't just a plain old C function pointer as it was before; now it is a block. So it can indeed capture the this pointer per the docs. That must have been what they were intending.

Add a Comment

Replies

No explanation is given as to why the context pointer was removed.

One possible reason may be that the block can capture the context pointer, so you have no need to get it from a parameter of callback function.

  • Good point @OOPer - I didn't notice that this isn't just a plain old C function pointer as it was before; now it is a block. So it can indeed capture the this pointer per the docs. That must have been what they were intending.

Add a Comment