Why does this code work on iOS 32 bit but not on 64 bit?
<p>The code below receives MIDI data from a musical instrument connected to the iOS device. It works fine on a 32 bit iOS device. On 64 bit, the callback function is also called for every event, but the data received in 'pktlist' is invalid. What is wrong?</p>

<p>The data I receive in <code>MidiReadProc</code> is always the same on a 64 bit device, and it is clearly wrong as the length would normally never be 0:</p>

<pre><code>pktlist^.numPackets = 1
lPacket.MIDItimestamp=$E4FE000100000961
lPacket.length=$0000
lPacket.data[0]=$00
</code></pre>

<p>The Callback function:</p>

<pre><code>procedure MidiReadProc(pktlist: MIDIPacketListRef; refCon, connRefCon: Pointer); cdecl;
var
  lPacket: MIDIPacket;
  lPacketRef: MIDIPacketRef;
  j: Integer;
  lPtr: ^Byte;
begin
  lPacketRef := MIDIPacketRef(@(pktlist^.Packet[0]));
  for j := 0 to pktlist^.numPackets-1 do
  begin
    lPacket := lPacketRef^;
    if (lPacket.length &gt; 0) and (lPacket.data[0] &lt;&gt; $F0) then
      //handle data here

    //translation of the MIDIPacketNext Macro:
    lPtr := @lPacketRef^.data[lPacketRef^.length];
    lPacketRef := MIDIPacketRef((UInt64(lPtr) + 3) and (not 3));
  end;
end;
</code></pre>

<p>In case it could be related to the header translation, here is the translation:</p>

<p>Extract from CoreMIDI.h:</p>

<pre class="lang-c prettyprint-override"><code>typedef UInt64 MIDITimeStamp;

#pragma pack(push, 4)
struct MIDIPacket
{
    MIDITimeStamp       timeStamp;
    UInt16              length;
    Byte                data[256];
};
typedef struct MIDIPacket           MIDIPacket;

struct MIDIPacketList
{
    UInt32              numPackets; 
    MIDIPacket          packet[1];
};
typedef struct MIDIPacketList MIDIPacketList;
#pragma pack(pop)

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


#if TARGET_CPU_ARM || TARGET_CPU_ARM64
// MIDIPacket must be 4-byte aligned
#define MIDIPacketNext(pkt) ((MIDIPacket *)(((uintptr_t)(&amp;(pkt)-&gt;data[(pkt)-&gt;length]) + 3) &amp; ~3))
</code></pre>

<p>Extract from CoreMIDI.pas (the translation of CoreMIDI.h is made by Pavel Jiri Strnad and <a href="http://pjstrnad.com/my-projects/delphi-implementation-missing-ios-frameworks/" rel="nofollow">is available here</a>):</p>

<pre><code>MIDITimeStamp = UInt64;

MIDIPacket = record
   timeStamp: MIDITimeStamp;
   length: UInt16;
   data: array [0..255] of Byte;
end;
MIDIPacketRef = ^MIDIPacket;

MIDIPacketList = record
   numPackets: UInt32;
   packet: array [0..0] of MIDIPacket;
end;
MIDIPacketListRef = ^MIDIPacketList;

MIDIReadProc = procedure (pktlist: MIDIPacketListRef; readProcRefCon: pointer; srcConnRefCon: pointer); cdecl;
</code></pre>

<p><strong>Update:</strong></p>

<p>As suggested by David in the comments, here are field offsets of the records:</p>

<p><code>MIDIPacketList</code> offsets: </p>

<pre><code>64 bit Align 8: numPackets=0 packet=8 &lt;- this was the one causing problems
64 bit Align 1: numPackets=0 packet=4
32 bit Align 8: numPackets=0 packet=4
32 bit Align 1: numPackets=0 packet=4
</code></pre>

<p><code>MIDIPacket</code> offsets: </p>

<pre><code>64 bit Align 8: timeStamp=0 length=8 data=10
64 bit Align 1: timeStamp=0 length=8 data=10
32 bit Align 8: timeStamp=0 length=8 data=10
32 bit Align 1: timeStamp=0 length=8 data=10
</code></pre>