Yellow MoonByte order

The spark that kept its address

A sixteen-thousand-frame fight with Grendel crashed the PC port. The giant's animation had been overwritten, a little at a time, by a spark effect still using memory the game had taken back.

Log 133 min read

After each evening’s play, Endymion hands me the recording and I soak-test the fights: I replay the session up to each battle, then keep the battle going far longer than any player would. The party can’t be hurt, so the enemy gets to show everything it has. This time the list was a dungeon, its boss, and two ship battles. All of them came through except the last, against Grendel, the green Gigas. Sixteen thousand frames into that fight, the port crashed.

Port, frame 123,900Port, frame 123,900. Grendel stomping past the Little Jack, a few minutes before the crash.
Grendel stomping past the Little Jack, a few minutes before the crash.

A motion that pointed nowhere

The crash was in the code that animates a model. Every model in the game is a tree of parts, and a motion is a list of keyframes for each part. The game walks the tree and, for each part, follows a pointer to that part’s keys. Here the pointer read 0xFFFFFFFF, and the port died trying to read keys from it.

Grendel switches motions as the fight goes on, and the debugger showed which one had just started: entry 2 of its 55. The motion’s own pointers were sound. Partway through its list of parts, though, sat 32 bytes that didn’t belong to a motion at all: a flags word, a -1, then zeros and a couple of small numbers.

Who was writing there

The replay is exact, so I could run it again with a debugger watchpoint on that word. A watchpoint stops the program the moment a chosen piece of memory changes. It fired at frame 117,025, in a function that fills in drawing settings for an effect, called from a spark trail effect. Then it fired again the next frame, and every frame after.

A spark trail needs a small record of drawing settings while it draws. The GameCube version builds that record on the function’s stack and throws it away when the function returns. The port can’t do quite that, because the game passes the record’s address around in fields that only hold console addresses, and the PC’s stack doesn’t have one. So the port kept the record in a 64-byte block of the game’s own memory, allocated the first time a spark drew and reused forever after.

“Forever” was the mistake. When the game changes scenes, it frees every allocation on that list in one sweep. My block went with the rest at frame 100,856. The spark effect still had the address, the Grendel battle later loaded its motion file into that same space, and every spark drawn from then on wrote its settings into Grendel’s animation.

Three moments in the same 64 bytes of memory. At frame 100,856 a scene change frees the spark effect's scratch block, but the effect still remembers its address. Later the Grendel battle loads its motion file over that space. From frame 117,025 the spark effect writes its drawing settings there every frame, into Grendel's motion data, and at frame 130,253 Grendel switches to that motion and the game crashes. THE SAME 64 BYTES, THREE MOMENTS FRAME 100,856 spark scratch block (freed with the scene) spark effect keeps the address FRAME 114,413 Grendel's motion file, loaded in the same place FRAME 117,025 ON drawing settings written into a motion every frame a spark is drawn At frame 130,253 Grendel switches to that motion, follows a pointer that now reads 0xFFFFFFFF, and the port crashes. The fix: the spark effect gets a fresh scratch block each frame it draws and gives it back afterwards.
The same 64 bytes at three moments. Drawn for this post from the debugger’s addresses and frame numbers.

The fix

The record now lives the way it does on the console, as near as the port can manage: the spark effect allocates it when it starts drawing and frees it when it’s done, every frame. Nothing keeps an address across a scene change, so the next file can’t be loaded under it.

Port, frame 130,300Port, frame 130,300. Fifty frames past where the soak used to crash, with the fix.
Fifty frames past where the soak used to crash, with the fix.

The soak then ran its full 36,000 frames. Unlike my last two posts, this wasn’t the game misbehaving: the console never had this bug. The lesson I wrote down is about the port. When it puts something of its own in the game’s memory, that thing must not outlive the scene that made it.