Closed Nycto closed 8 months ago
I started playing around with using the playdate realloc
method for everything in Nim: https://github.com/Nycto/playdate-nim/commit/memory
I've got things working on the simulator, but everything immediately crashes when trying to use it on device. Feel free to poke at this and see if it makes sense.
I thought the _malloc_r
, _realloc_r
and _free_r
definitions in setup.c
(SDK folder) were enough to override the default malloc strategy, but I'm not sure if this is really the case.
What makes you think Nim wasn't using the SDK provided functions?
This might just be a simulator thing, actually. When I completely comment out all the memory allocation functions in setup.c
, I'm still able to compile and run the simulator. But I just tried compiling for the device and it looks like it's failing appropriately
Ok, yes seems like compiling for the simulator just uses the std functions no matter what.
Maybe we should also try reverting back to switch("os", "any")
compiling for the simulator too.
I remember removing this setting time ago.
I have to admit to being a bit stumped so far. I’ve tried various compiler and linker flags, but nothing seems to impact the simulator. If you get a chance to look at this, I would love a second set of eyes.
Since I was stuck, I switched back to working on this branch: https://github.com/Nycto/playdate-nim/tree/memory
I got it mostly working, though I still have one workflow failure to investigate. I think this might be the right path forward as it’s the official Nim way to override malloc.
I’m going to do some testing this morning to see if it resolves my memory corruption issues
Hiya!
I've been working on debugging a device-only crash that I think is related to memory corruption. I started a thread about it on the playdate forum here:
https://devforum.play.date/t/device-crash-for-c-nim-based-game/16205
While investigating, I noticed that it doesn't appear that Nim is using the
malloc
implementation provided by the playdate SDK. I tried adding logging into the memory allocating methods insetup.c
from the Playdate SDK and they were never being called. It look like Nim directly callsmalloc
from<stdlib.h>
, as seen here:https://github.com/nim-lang/Nim/blob/version-2-0/lib/system/mm/malloc.nim#L5
Does that sound right? Have you looked into overriding the default Nim malloc implementation to use playdate's realloc? Or have I got this all wrong?