Memory Leak...need help

Blitz3D Forums/Blitz3D Programming/Memory Leak...need help

OrcSlayer(Posted 2004) [#1]
Well, I've been working with Blitz for quite a while now and finally I caused a memory leak. Sadly, I created a large part of my game engine before I realised it was there. I added a counter to show me video memory usage, and as I expected the available memory drops like a rock as soon as the game fires up. At first it crashed me after a couple minutes, but now it just saps it down to five-six megs and gets really sluggish. I have no clue what could be responsible, so what I'm asking is, what is the best way to track down memory leaks? I've already checked for rampant entity creation...nothing there. Particles, projectiles, and effects rarely exist in the game for more than a second, and there is nothing else that I know of that could be a cause for this. How should I go about debugging this? Thanks in advance for any suggestions.


RiverRatt(Posted 2004) [#2]
I just read something like this in blitz coder. Don't know if it will help but...
It says, you should always return a value from your fuctions. Even your destructor functions.
"By having your Destroy function return a false value when you try to pass it a null type instance, its one more clue to help you track down where things are going bad in your code. Management functions can return how many objects they've processed, and just about any routine can return a true or false based on whether the operation succeeded or not." by Kryler
Well thats all I got. Hope it helps.


OrcSlayer(Posted 2004) [#3]
Hmmm, I'll try that. Somehow I get the feeling this is going to be nasty to deal with...


Damien Sturdy(Posted 2004) [#4]
just track all created meshes, and make sure youre freeing them before you "re"create them. same with sounds- also, remember sounds dont get cleared at a "graphics" command....


OrcSlayer(Posted 2004) [#5]
Well, ruling one thing out, I haven't implemented sounds yet...

As far as freeing entities, I'm 90% sure I do that for everything, but now that you mention it...I'll check through everything and make sure I free the meshes and sprites...I'm pretty sure I'm doing that though. Unless I accidently screwed something up and didn't notice it (wouldn't be the first time).


Curtastic(Posted 2004) [#6]
make sure you do not have any loadtexture/loadsprite/loadimage in your loop or createparticle() or anything like that.


Hujiklo(Posted 2004) [#7]
Not generating any invisible pivots are you? - I watched my frame rate go from 60 to 2 in the space of 30 seconds by generating non stop pivots by accident.


OrcSlayer(Posted 2004) [#8]
Well, some projectiles are invisible pivots, but they are freed and deleted just like the mesh and sprite ones. Odd thing is that the memory starts to drain the instant the game starts, before any projectiles/other entities have had a chance to be created anyway...


MSW(Posted 2004) [#9]
Are you textureing your models/levels in code?

When loading certain 3D model files with texture links, the loadmesh() function will try to load and apply the associated textures too...


IPete2(Posted 2004) [#10]
Orc,

The key to solving a problem of this nature is to be thoroughly logical about the search for it. You cannot afford to mess up so start one morning fresh and clear headed, determined not to stop until you find it. You will find it.

Here is how I would possibly approach this problem.

Set up a counter. Everywhere you create something, increment this counter. Everywhere you delete, free, destroy something, decrement this counter.

Run the game and check that the counter is increasing conitnually. Then you will have proved that is the problem. Until then do not believe that this is the problem. Before you attempt to spend a lot of time to solve a problem, you should first prove that the problem you think you have, actually exists in the area you believe it is in.

Then try one by one to turn off each system in you game. Watch the counter carefully. You should find the system which is causing the problem. Then remove the counter from everywhere else and leave it in just that system.

From the top, down remove the counter one by one to find the actual problem.

Hope this helps!

IPete2.


OrcSlayer(Posted 2004) [#11]
Well, setting aside today to run my search...I'll take all your advice into account and see if I can pull this off. Thanks for all the support guys, I'll let you know how it works out.


OrcSlayer(Posted 2004) [#12]
Well, results are in...whatever is causing it is in the main game document (the one that contains the main loop and adds everything else with includes). The strange thing is it's almost identical to all my other former projects, and they never had this issue...

At least I have a much smaller area to search now. When I figure out what stupid mistake I made to cause it, I'll let everyone know. Thanks again for all the suggestions!


OrcSlayer(Posted 2004) [#13]
Ok, fixed it...I feel like the worlds greatest idiot...

Somewhere towards the end of the main loop, it was running:

LoadFont("Ariel",10)

EVERY SINGLE FRAME.

No wonder 256 megs of available memory got sapped down to nothing in a couple minutes.

Somehow I feared this would turn out to be the result of a really stupid mistake...*sigh*


big10p(Posted 2004) [#14]
Oh dear. :)


OrcSlayer(Posted 2004) [#15]
I could have sworn I put that before the main loop...then again it was six AM when I wrote that if I remember correctly...ick.


aab(Posted 2004) [#16]
l O l
My friend done that two weeks ago!


OrcSlayer(Posted 2004) [#17]
The moral of this story: Drink plenty of caffine if you want to risk writing code early in the morning. If I was on a caffine high I doubt this would have happened :-P


big10p(Posted 2004) [#18]
Anyway, well done for having the balls to admit to this gaff. This kind of thing happens to everyone, at some point. :)