Hardware crashed with "rotatedImage creation failed" in SDK 1.4

I know this is similar to Simulator crashing for some sprite rotations, but I can’t tell if it’s exactly the same. In any case, it’s definitely happening in 1.4.0, running on Playdate hardware.

I haven’t extracted this into an example project yet, but the code is in the repo I just pushed to the Panic gitlab for Spellcorked.

Even though these are labeled “sprite”, they aren’t SDK Sprites. They’re just cached images from a 207 x 206 px imagetable. Basically each frame the deg increments up or down by 1, and if it’s divisible by 10, I rotate the image and store it as rotSprite.

function StirringScene:rotateCreamSprite()
  local deg = self.cream.rotDegrees
  deg = deg + self.lastCrankDirection
  -- print('rotateCreamSprite() new deg: ' .. deg)
  if deg % 10 == 0 then
    self.cream.rotSprite = self.cream.sprite:rotatedImage(deg)
    -- print('rotateCreamSprite() saved new sprite')
  end
  self.cream.rotDegrees = deg
end

The crashing line is self.cream.rotSprite = self.cream.sprite:rotatedImage(deg). Here’s the bug report:

I’ve done a lot of work this past week to avoid crashes caused by calling the built-in image functions in uncertain conditions. This one, I can’t figure out. If these calls would fail with a warning (or return false if they fail) instead of crashing the entire app, it would make my life a lot easier!

Is there any way for me to code, “Try to rotate this image, but if it doesn’t work, no big deal? Just return false and keep the old image, I don’t care”?

Thanks y’all!

This will work better in the next update! What isn’t obvious at all is it failed because we couldn’t allocate memory for the image. I’ll fix that message to make it clearer. And hopefully you won’t run into that again anyway because I’ve added a step in the memory allocator where it’ll run a garbage collection cycle and try again if it can’t get the requested amount of memory. Until then, a clumsy workaround is to call collectgarbage() before rotatedImage(). I’ll also file a feature request to change that so it returns nil plus an error message as a tuple, like a number of other functions do.

1 Like

Ah, thanks so much, @dave! That makes sense; managing the memory related to some of these large images has been an ongoing challenge, but I haven’t really delved into manual garbage collection. I’ll add that and also look forward to the update.

Really appreciate it!

1 Like