crash when reloading images after Discard()

Monkey Forums/Monkey Programming/crash when reloading images after Discard()

Anatol(Posted 2012) [#1]
Hi,

I spent quite some time trying to track down a bug, but no luck so far. Maybe someone has a hint...

In brief: my app is very likely to crash across all targets when I discard an image from a global object and then reassign it again. The app then crashes when I try to call DrawImage().

Error messages in HTML5 are "Monkey runtime error: TypeError: Unable to get value of the property 'image': object is null or undefined", iOS crashes with a segmentation violation, other targets with similar messages.

So quite clearly the image object seems to be null.

In length: I'm working on an interactive book project that on a page turn preloads resources of previous and next pages in advance and discards resources of other pages.

It all works fine, but some pages use a global Particles array that contains a fixed number of particle system objects. I used a global array so that I only need a smaller number of particle system instances and not one or more for each page, so basically I recycle these particle systems.

A page populates a free particle system, so the page uses a simple
particleImage = LoadImage(path)

' and later populates the Global Particles system like this:
Particles[id].AddParticle(particleImage, position, velocity, size, etc)

The particle system does its particle calculations and renders each particle in Particles[id].Render(); this method simply loops through all active particles and then calls DrawImage(). If I come back to a page that had its resources discarded earlier this is where the app crashes. I even added a special precaution to check if the image is Null:
If image[index] <> Null
	DrawImage(image[index], position[index].X, position[index].Y, rotation[index], size[index], size[index], frame[index])
EndIf

but this doesn't seem to help. In 4 out of 5 cases it crashes.

I can draw the particleImage on the page directly without any errors, but there seems to be an issue when I try to pass the image again to the particle system object when I populate it (see code above: AddParticle()).

On a page turn I clean up the particle system that was used. I set the particle image to null.
image[index] = Null

I hope the code I post here is not too vague, but the entire code is too huge to post and still a bit messy in some places.

I would appreciate any hints and insights.


muddy_shoes(Posted 2012) [#2]
The error message is incorrect. It's not the image reference that's null, it's the internal surface reference.

You need to reload the image if you want to use it after you've called Discard.

Edit: Also, it might be an idea if the Loaded check was changed to handle the possibility that surface is Null and allow it to be called to check for this condition, i.e.:




Anatol(Posted 2012) [#3]
Thanks muddy_shoes. Yes, you're right, it actually crashes in the graphics module on the line
context.device.DrawSurface image.surface,0,0

I do reload the image though. In my page module I call
particleImage = LoadImage(path)

before I pass it again to the global particle system object. I'm sure that the image is loaded because I can draw it in the page object, but not in the particle system object that the same image was passed to.

Or maybe I misunderstand you?


muddy_shoes(Posted 2012) [#4]
Without seeing your code I can't verify if your reloading is correct. The error suggests that the reference in your draw call has been discarded and not reloaded so I think you should look again.


Anatol(Posted 2012) [#5]
> I think you should look again.
Hm, yes. I think so, too, I should look again. It seems that under certain circumstances the program adds the particle (and particle image) to the particle system before the image was reloaded. It's still a mystery to me where and why this happens, but I'm quite confident that I'll find it. Thank you!


muddy_shoes(Posted 2012) [#6]
I'd be happy to look at the code if you're willing to share. Today seems to be a bug hunting day for me anyway.


Anatol(Posted 2012) [#7]
Hi muddy_shoes. Thanks for all your help and for offering to go on a bug hunt! That's really appreciated. I read this a bit late so I figured it out in the meanwhile.

As expected the issue was my program code and the order in which things were loaded and populated.

Sometimes the app was setting up the particle system of a page before it had loaded the resources, hence the image was null. When I figured that out I still got the same error but less frequently. This time it was because under certain circumstances the program didn't set a flag to reinitialise a page, so resources were loaded but the particle system was not set up properly which made the whole thing crash again, because the particle system had a null object as the particle image, but this time for other reasons.

Thanks, muddy_shoes! I've learnt something new again.
> The error message is incorrect. It's not the image reference that's null, it's the internal surface reference.

Before that I was looking for the wrong cause of the issue.

Most parts of my project are reasonably clean I think, but not the page turn code which is quite confusing. It would be best for me to rewrite this from scratch at some point...