I have a program (one with quite a few lines of code at this point… about 14,000 lines in 81 classes) which is freezing when calling saveLocalData(). It works fine the first time saveLocalData() is called, but freezes for several minutes until crashing on a subsequent call.
The “use case” is basically that the user can save and load game maps. The save involves three calls to saveLocalData(), one to save the map itself, one to save the updated list of maps, and one to save a thumbnail image for the map. Creating a new game map and saving it works fine when you first run the program, but any attempt to save another map causes the aberrant, fatal behavior. The program freezes for about two minutes, and ultimately crashes Codea entirely (not just the running Lua program, but all of Codea).
I’ve tried to isolate this to a problem in my own code. I call the immediate belief that someone else’s code is responsible “black box syndrome”, i.e. the idea that the problem must be in whatever part of the code the programmer has no insight into or control over, but I’ve worked very hard to be sure that it is not my doing, and it clearly is not (unless I’ve missed something, like a call to the system funciton pleaseDoNotCrashWhenSaving(true)). If I simply comment out all calls to saveLocalData(), it runs fine (but, of course, without actually saving anything). If I put print() statements in to trace where the code is hanging, I get inconsistent results, depending on where I put the statements, so I can’t even localize it specifically in the call to saveLocalData(), but I imagine this is an artifact of buffering with print to the console.
My only thoughts are either that saveLocalData() is not properly closing opened resources, so it is running out of memory, or else some side effect of the fact that my saveLocalData() calls are being made within a coroutine (so that I can show a “saving” animation while it works). I don’t think the coroutines are the problem, though, because I commented that out to let it run “in the main thread”, and that had no impact.
At the moment, the only workaround I can see to this problem (barring a fix in Codea) is clumsy; use raw file io operations to read and write the files myself. I really don’t want to have to do this, but I may do it, just to guarantee that it works, and so the problem really is in saveLocalData(). I’m busy writing a reusable wrapping-scrolling-editable text-box widget, however, and that is far more fun than coding up low level IO read/write calls.
[BTW, I’ve started a blog to outline my adventures in Codea, and more importantly to share a lot of the reusable code I’ve written, such as my class serializer and my UI components, but I haven’t yet created a GitHub project for it, or even found time to write meaningful posts. It’s so much more fun to write code than to write about writing code. I’ll post a link to the blog when I get something tangible out on it.]