The problem is that Byte packetBuffer[size+100] declares a local array that should not be accessed after this method completes. You have two options (which I will write as functions):
Option 1:
MIDIPacketList *makePacketList(const UInt8 *data, UInt32 size) { Byte *packetBuffer = malloc(size + 100); MIDIPacketList *packetList = (MIDIPacketList*)packetBuffer; MIDIPacket *packet = MIDIPacketListInit(packetList); MIDIPacketListAdd(packetList, sizeof(packetBuffer), packet, 0, size, data); return packetList; }
If you do it this way, you will need a free() buffer later, which is a little painful.
Option 2:
MIDIPacketList *makePacketList(Byte *packetBuffer, const UInt8 *data, UInt32 size) { MIDIPacketList *packetList = (MIDIPacketList*)packetBuffer; MIDIPacket *packet = MIDIPacketListInit(packetList); MIDIPacketListAdd(packetList, size + 100, packet, 0, size, data); return packetList; }
In this case, you will have to declare Byte packetBuffer[size + 100] outside the function and pass it as the first argument, which is also somewhat inconvenient.
Chuck source share