Attempt to index a nil value in function 'newImageRect'

Hi guys, I’m getting the following error when trying to create image from system.TemporaryDirectory. Image was created when saving snapshot using display.save() function.
I dont know how to reproduce the error, because it doesn’t happen every time, but it keeps popping in the google crashlytics.

I create the image when coming back from suspend - on application resume event:
display.newImageRect(image_name, system.TemporaryDirectory, image_width, image_height)

And I also have the exact data that makes the error which shows that nothing is nil:
display.newImageRect(“tmp_snapshot_1.png”, userdata: 0x7f86f83c6a, 250, 250)

Error: ?:0: attempt to index a nil value
stack traceback:
?: in function ‘newImageRect’

So what could be the reason to get the following error?
I checked the source code of newImageRect function and I have no idea where it could have returned that error.

Thank you in advance for any ideas.

Forgive if I’m missing something but what is userdata: 0x7f86f83c6a in display.newImageRect?

Also, we could use sample code and instructions to try and see what happens.

My guess is the error is actually saying you’re trying to use the object created by that call and the variable you stored the reference to it in is nil.

Check the spelling of the file name. It must match exactly and it must be the same case.

If it isn’t that, then I suspect the image file is not present in the temporary directory when you try to use it.

The temporary files may be getting wiped/deleted while the app is suspended.

@bgmadclown The userdata is from print(system.TemporaryDirectory) just before the error happens.
I could provide a sample code, but as I said - I don’t know how to reproduce the error because it never happened to me. I just se it on google crashlytics logs and with lot of users even a rare bug can happen many times.

@roaminggamer The error happens in newImageRect function while creating the image. The spelling should be ok because image name gets saved in variable while saving the image.
But I think you might be on to something - I think that it might be that temporary files get deleted on some devices while in suspend.
The main problem is that the error in debug build says: attempt to index local ‘imgr’ (a nil value)
And I had to test it in production build to get: attempt to index a nil value

I just don’t get it why production build error doesn’t say: attempt to index local ‘?’ (a nil value)
Thats why I couldn’t find how to get that error because it’s different in production than in debug.

As you seem to have already realized, you can’t rely on files in system.TemporaryDirectory to remain between sessions. Instead, use system.DocumentsDirectory and I’m pretty sure that your crashes will go away. The API documentation hints about this without being too specific, which I suppose is because the actual behaviour on different devices is unpredictable.

About the error you see in the end, I guess it has to do about the implementation of newImageRect(). Haven’t had a look at the source code myself, but it sure sounds like a missing nil check when the image file can’t be found.