Maybe, almost sure, that is the issue of memory leaking. I’ll try to solve it, using the timer and let a feedback. Thank you, Raphael.
Corona is too fast for itself sometimes, which is normally a good thing. I’ve learned to never switch scenes without a delay of 500-1000ms, as I’ve always experienced some weird crashing due to variables or objects not fully unloading/deleting/disposing.
I"ve realized it. Corona is very fast! OMG! hahaha
I found out it’s not ( REALLY NOT! ) good using **display.remove(object); object = nil; **on the removeOffScreenItems function, because thus the function will not delete the objects; using object:removeSelf() worked perfectly and I took a day to find out it! hahahahah Oh God!
Even with the delay (no matter if I put 5 seconds) before changing screens, the memory usage grows from screen to screen… and I’m removing the listeners, nil’ing my localGroup and the tables…
Hi @WilerJr,
Can you post your “cleanup” function code again? It has likely changed a bit since you posted it above. Maybe we can see something suspicious inside it…
Brent
For the life of me, I couldn’t understand why my new game started crashing all over the place. Thought it was a Daily Build (even went back to 971 and it crashed a lot less, but eventually did), but when I saw WilerJr’s post, I got desperate and changed all my display.remove( object ) functions with if object.removeSelf then object:removeSelf() end.
Needless to say, it stopped crashing (I even let my iPad run my app and my MacBook Air run my app in the Simulator all night), but Tom at Corona says that section of code hasn’t been changed in a long time.
Go figure. At least I can continue to move forward with development. This one was a show-stopper!
Nothing has changed with display.remove. Here is the internal code and you can see that it checks for a nil object and then verifies that the object has the removeSelf method attached.
-- convenience wrapper for object:removeSelf() that eliminates check for a receiver that's nil display.remove = function( object ) if object then local method = object.removeSelf if "function" == type( method ) then method( object ) -- same as object:removeSelf() end end end
function removeOffscreenItems() for i = #images, 1, -1 do local oneImage = images[i] if(oneImage.alpha == 0 or removeOffscreenItemsController == true)then oneImage:removeSelf(); table.remove( images, i ) ; end end end
This is where I remove the images created by
background:addEventListener("touch", creator);
removeOffscreenItemsController is a control variable to verify if the freeEverything function is running and clean the images when its listener to alpha is already removed. At the end of freeEverything, I call a timer that changes the screen after 500~1000 miliseconds.
This is my creator function:
function creator(e) if(e.phase == "began")then images[#images + 1] = display.newImage(........) image = images[#images]; transition.to(image, {alpha = 0}); localGroup:insert(image); end end
I think the problem is the transition, but I can’t cancel it… is there a way to link a image to a specific transition so I can remove it?
Edit: I created a transitions table and I’m trying to make it work…
http://docs.coronalabs.com/api/library/transition/to.html
“This function returns a tween reference corresponding to the animation (or interpolation) of the object target based on property values specified in params.”
In other words, you want to be able to reference the transition action in the likely event that you need to cancel it.
Try simplifying the function to this:
function creator(e) if(e.phase == "began")then local newimageindex = #images + 1 images[newimageindex] = display.newImage(........) images[newimageindex].trans = transition.to(images[newimageindex], {alpha = 0}); localGroup:insert( images[newimageindex] ); end end
Now, the transition is captured in a property variable called trans. At any point - whether the transition is in progress, or even if the transition is over - that trans variable still exists under that particular image. So, you can then sweep through the entire roster of images and cancel them out:
for i = 1, #images do if images[i].trans then transition.cancel( images[i].trans ) end end
You can do it for timers as well, just place as a property of the image itself:
images[newimageindex].timer = timer.performWithDelay( 100, doSomeArtificialIntelligence ) ... if images[i].timer then timer.cancel( images[i].timer ) end
I’m using this object[newObjectIndex].trans you taught, but didn’t solve the problem.
It’s becoming even harder to deal with that issue… ;s
Take the example: I go into a screen, then use creator function to create images, right? In that time, let’s supose, the memory usage is 316. I create images and the memory usage grows. So the images are transition’ed to alpha = 0 and the removeOffscreenItems works, turning down the memory usage, but… it does not come back to 316; it comes back to a number like 339.
And a incredible thing: the table.getn(images) shows a value 0 when printed after the work of removeOffscreenItems.
If I create a huge quantity of images then go back to menu, the memory usage shows a value like 400; if I create nothing then go back to menu, the memory usage show a value like 350! What’s wrong here if I’m removing all the objects (by localGroup:removeSelf()) and all the listeners?
I think I’m about to fix that problem, but I need to know what’s missing…
Hey! Don’t forget this topic!