Issues cleaning up memory leaks

When using sprites made in spriteloq, I haven't been able to get the unrequire() function to work. It always comes back unrecognized and gives me an error. As a result, this is giving me memory leaks.

I'm using it in conjunction with director class, and so after using a few sprites on one screen, I'll head to another screen and use some other ones, and on and on... each one increasing the amount of memory used (not texture memory, the other kind). I'm getting crashes after a few minutes of play. My game is really animation heavy, so this is the bulk of my performance limitations.

Any advice on this matter? I know memory leaks are one of the most slippery problems that we deal with, so any tips, however tangential, would be much appreciated.

Thanks,

-Matt
W2MD

Are you calling unrequire() immediately after a director scene change?

This work for me.

director:changeScene("level1")
unrequire('level1')

Notice double quotes on first line and single quotes on second line.

Do you use a clean function in your various director level files? One thing I've discovered is that the clean function never gets called if you do this unrequire.

Are you disposing of your sprite factories before you leave each level?

I was trying to unrequire the lua file created by spriteloq, but unrequiring the scene isn't working for me either. Here's what I've got. This should answer your questions better than my prattling. As for the names of things, "TitleScreen" is the scene I'm leaving, and "targetPractice" is the one I'm going to. pManFactory is the spriteloq sprite factory, pMan is the sprite. And I'm getting an error at unrequire() in the last line of the last function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
        --====================================================================--
        -- CLEAN
        --====================================================================--
        
 
        --this is preinitialized
        function cleanTitleScreen()
 
                --adding objects to localGroup so that they will be cleaned after leaving the screen
                addObjectsToLocalGroup()
 
                Runtime:removeEventListener( "enterFrame", move )
                audio.stop(footstep1Sound)
                audio.stop(footstep2Sound)
                audio.dispose(footstep1Sound)           
                audio.dispose(footstep2Sound)
 
                pManFactory:dispose()
 
                pManFactory = nil
 
                if pMan then
                        pMan:removeSelf()
                        pMan = nil
                else
                        pMan = nil
                        print("ERROR: pMan doesn't exist on the TitleScreen?!?")
                end
                print("BG destroyed")
 
        end
        
 
        
        local function targetPracticeButtonRelease()
                if justOnce then
                        justOnce = false
                        print("character")
                        cleanTitleScreen()
                        director:changeScene("targetPractice", "crossfade")
                        unrequire('TitleScreen')
                end
        end     

@w2md, I use performWithDelay when I change scene. Here's typical scene change routine that I use. I haven't noticed any memory leak issue with this:

1
2
3
4
5
6
7
8
9
10
11
-- when changing to a screen that has no need for spriteFactory
function changeScene(event)
        local function launchWithDelay()
                spriteFactory:dispose();
                spriteFactory = nil;
                director:changeScene("levels", "flip");
                unrequire("level1.lua");
        end
        -- a few lines of code here for things that need to happen before timer launches
        timerStash.newTimer = timer.performWithDelay(100, launchWithDelay);
end

@Naomi,

Unrequire still gives me this error:

Runtime error
...ini3/Desktop/W2MD/Projects/GumChucks/TitleScreen.lua:194: attempt to call global 'unrequire' (a nil value)
stack traceback:
[C]: in function 'unrequire'
...ini3/Desktop/W2MD/Projects/GumChucks/TitleScreen.lua:194: in function 'onRelease'
...sers/MacMini3/Desktop/W2MD/Projects/GumChucks/ui.lua:97: in function <...sers/MacMini3/Desktop/W2MD/Projects/GumChucks/ui.lua:34>
?: in function <?:215>

I even changed the way I was writing it to match your format (i.e. unrequire("TitleScreen.lua") ) and used a timer as well.

My memory usage for the TitleScreen is 470 kB when I first open up the app (it's my first screen). And targetPractice starts off at 147kB if I go straight there from main.lua instead of going to TitleScreen first. However, after my director:changeScene to targetPractice from TitleScreen my memory usage is up at 505 kB. And everytime thereafter when I go back to the TItleScreen my memory increases by about 30 kB.

Any other suggestions? I really appreciate you guys ironing this out with me.

Thanks,

-Matt
W2MD

Do you have the latest version of the API? The error says unrequire doesn't exist which probably means you're running an older version of the API. FYI, unrequire is a function in loq_util. It's not part of the Corona API.

If you're seeing an increase in your memory from repeatedly loading the same module, it sounds like you have a memory leak as well.

@w2md, a quick note to let you know that I added the second type I use. See comment #3 with edits.

Thanks Don,

Changing out the spriteloq api files made a big difference just by itself. Now the memory only goes up to 350 kB when I transition to the targetPractice scene. I did notice that unrequire() doesn't clear up any memory at all, which makes this even more confusing. What is it that is typically unrequired? The scenes that director is cleaning up? Or is it the spriteFactories or the sprites themselves?

Thanks!

-Matt
W2MD

The purpose of unrequire is to reclaim memory from modules you no longer need. So for example in my game I have levels has modules. When the player finishes playing level 1, it's unlikely they'll go back and play level 1 again so I unrequire it to reclaim that memory. Depending on what you have in your level files that could be significant.

It only matters when you start having hundreds of levels though. And the clean up isn't 100%, but should be a significant reduction.

UPDATE: I also perform an unrequire inside of the Spriteloq API when you dispose of SpriteFactories to reclaim the application memory used by the loaded spritesheet metadata. If you're loading lots of sheets and levels, this can add up. Is it significant enough to fret over it? Maybe, maybe not. But when you do get a real memory leak it does make it easier to diagnose misbehaving code.

That explains everything really well. Thanks so much Don. It's great working with well made software that cleans itself effectively.

I am running into a little issue with the new api modules though. When I put loq_sprite.lua in, my retina-display and non-retina-display sprites are unusually large. It doesn't seem to effect the iPad build I made at all. It's probably some really simple difference in the code of the api.

Is there somewhere that lists off the differences in each version of the api libraries?

Thanks,

Matt
W2MD

Sorry, double post.

Hi w2md, I usually post to twitter or to the forum whenever I update the API. It's pretty infrequent. Whenever there's a major update I send an email to the mailing list.

The next update will include a major API change along with an update to Spriteloq. There are two branches of the API. One for those using an old version of Corona that don't include features in teh daily builds and one that supports the latest daily build. So if you're using build 591 or early there's a separate link on the downloads page for you.

There'll be a twitter post, forum post, blog post, and mailing list post when the next update occurs.

I responded to you in email already, but for anyone else curious as to what the issue this is referring to it's about dynamic spritesheet resolution / scaling. The next Spriteloq and API update will include the ability to export you swfs at different scales and have them incorporated into your projects easily!

views:2095 update:2011/10/19 14:58:09
corona forums © 2003-2011