Memory Leak

Need help in general for programming? Ask here.

Memory Leak

Postby Nishok » Fri May 11, 2012 9:51 am

Hello people,

My program has a memory leak somewhere, but I have no idea where to search. Do you guys have any tips on how to find out what causes the memory leak?

For some reason it counts 1-2MB per second to the process of my game, here is a screenshot of it reaching 1.7GB RAM usage after 10 minutes or so: http://content.screencast.com/users/Nis ... 1_1544.png

Thanks in advance.

Edit:

I found out that in CApp_OnRender.cpp I have this:
Code: Select all
CTTF::TTF.OnRender(Surf_Display);

This code draws the text on the screen, but when I comment out this code, the memory leak is gone.

So I thought it obviously has something to do with CApp_OnCleanup.cpp that my cleanup code for the TTF is not correctly done, so I went checking and saw this code from the main tutorial:
Code: Select all
SDL_FreeSurface(Surf_Display);

Now, this should obviously cleanup the whole Surf_Display, including the TTF because it was printed on Surf_Display, right? Well it seems not to do it correctly.
What am I doing wrong here?
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Memory Leak

Postby Nishok » Fri May 11, 2012 1:38 pm

Bump so you can see the edit.

I WILL remove this post as soon as someone has replied (to keep this forum clean obviously ^^)
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Memory Leak

Postby Tim » Fri May 11, 2012 5:50 pm

The reason is because SDL_TTF creates a surface every time you render your text. So, after you draw your text to the screen you need to free it. You don't want to free Surf_Display, because that's the main window surface.
User avatar
Tim
Site Admin
 
Posts: 255
Joined: Tue Oct 28, 2008 9:55 am
Location: Indiana

Re: Memory Leak

Postby Nishok » Fri May 11, 2012 6:55 pm

Thank you, I did not know SDL_TTF would keep on drawing it forever :x

Edit: Please remove my second post in this thread, it looks like the delete button gets removed after someone replies to that message xD
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Memory Leak

Postby Nishok » Sat May 26, 2012 7:14 pm

Sorry for the bump, but I have another (different) memory problem, but I did not want to make yet another thread for such a similar problem.

Basically every entity I make uses about 12MB RAM, which is WAY too high, especially if I need to spawn about 100 enemies (which hits 1.2GB RAM usage in total..)

I need help fixing this because it also takes about 5 seconds for my game to launch because of the high RAM usage.

Here is the code which loads the image file and puts it inside the entitylist from inside CApp_OnInit.cpp:
Code: Select all
char character[20] = "characters.png";
if(Orc.OnLoad(character, 32, 36, 3) == false) {
        return false;
}
CEntity::EntityList.push_back(&Orc);


Here is the code where it renders the images from the entitylist from inside CApp_OnRender.cpp:
Code: Select all
for(int i = 0;i < (int)CEntity::EntityList.size();i++) {     //To spawn the entities.
        if(!CEntity::EntityList[i]) continue;

        CEntity::EntityList[i]->OnRender(Surf_Display);
}


And here is the FULL code of my COrc.cpp file:
Code: Select all
#include "COrc.h"
#include "CApp.h"

COrc::COrc() {
    X = 0;
    Y = 0;

    Width = 0;
    Height = 0;

    MoveLeft = false;
    MoveRight = false;
    MoveUp = false;
    MoveDown = false;

    CurrentFrameCol = 3;
    CurrentFrameRow = 0;

    MaxSpeedX = 10;
    MaxSpeedY = 10;

    Health = 2;
    Interval = 0;
    AttackDelay = 2;

    Type = ENTITY_TYPE_ENEMY;
}

bool COrc::OnLoad(char* File, int Width, int Height, int MaxFrames) {
    if(CEntity::OnLoad(File, Width, Height, MaxFrames) == false) {
        return false;
    }

    return true;
}

void COrc::OnLoop() {
    CEntity::OnLoop();

    if((((X + 200) > app->AccuierePlayer().X) && ((X - 200) < app->AccuierePlayer().X)) && (((Y + 200) > app->AccuierePlayer().Y) && ((Y - 200) < app->AccuierePlayer().Y))) {
        if(((X) > app->AccuierePlayer().X)) {
            MoveLeft = true;
        }else {
            MoveLeft = false;
        }

        if((X) < app->AccuierePlayer().X) {
            MoveRight = true;
        }else {
            MoveRight = false;
        }

        if((Y) < app->AccuierePlayer().Y) {
            MoveDown = true;
        }else {
            MoveDown = false;
        }

        if((Y) > app->AccuierePlayer().Y) {
            MoveUp = true;
        }else {
            MoveUp = false;
        }

    }else {
        MoveDown = false;
        MoveLeft = false;
        MoveRight = false;
        MoveUp = false;
    }

    Interval += CFPS::FPSControl.GetSpeedFactor();

    if(Health <= 0) {
        Dead = true;
    }
}

void COrc::OnRender(SDL_Surface* Surf_Display) {
    CEntity::OnRender(Surf_Display);
    if(Surf_Entity == NULL || Surf_Display == NULL) return;     //If we don't have anything to draw, then we skip this function

    CSurface::OnDraw(Surf_Display, Surf_Entity, X - CCamera::CameraControl.GetX(), Y - CCamera::CameraControl.GetY(), (CurrentFrameCol + Anim_Control.GetCurrentFrame())  * Width, (CurrentFrameRow) * Height, Width, Height);
}

void COrc::OnCleanup() {
    CEntity::OnCleanup();
}

void COrc::OnAnimate() {
    if(SpeedX != 0 || SpeedY != 0) {
        Anim_Control.MaxFrames = 3;     //If our speed is higher than 0, then the maxframes will be 3 (so you'll get the animation).
    }else{
        Anim_Control.MaxFrames = 0;     //If your speed is 0, then there will be no animation.
    }

    if(MoveLeft) {
        CurrentFrameRow = 3;    //Makes the animation look left
    }
    else if(MoveRight) {
        CurrentFrameRow = 2;     //Makes the animation look right
    }
    else if(MoveUp) {
        CurrentFrameRow = 1;     //Makes the animation look up
    }
    else if(MoveDown) {
        CurrentFrameRow = 0;     //Makes the animation look down
    }

    Anim_Control.OnAnimate();
}

bool COrc::OnCollision(CEntity* Entity) {
    if(Entity->Type == ENTITY_TYPE_PLAYER) {
        Dead = true;
    }

    if(Entity->Type == ENTITY_TYPE_PROJECTILE) {
        Dead = true;
        if(Interval > AttackDelay) {
            app->AccuierePlayer().Kills += 1;
            Interval = 0;
        }
    }

    return true;
}


Can anyone help me with this?
If you think the issue lies within another code snipped, just ask and I'll place more code.

Thanks in advance
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Memory Leak

Postby Peter » Sun May 27, 2012 5:09 am

If more than one enemy use the same image you could let them all share the same SDL_Surface.
Peter
 
Posts: 45
Joined: Sun Nov 28, 2010 3:14 pm

Re: Memory Leak

Postby Aaru » Sun May 27, 2012 1:49 pm

Yeah, like peter said. If you played my game you would notice it start to lag hardcore after a few waves. It's because I loaded a separate surface for each enemy. Which sucked up the memory. So like peter said. You want to create a static sdl_surface or something and set all your enemies = to that surface. Not sure if that's how you are suppopsed to do it though lol.
User avatar
Aaru
 
Posts: 72
Joined: Fri Sep 10, 2010 3:56 am
Location: California

Re: Memory Leak

Postby Nishok » Wed Jun 06, 2012 4:48 am

Your solution was correct, it kept on reassigning the image for every new entity. I fixed this now, thanks ^^
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am

Re: Memory Leak

Postby marvin » Wed Jun 06, 2012 6:04 am

Nishok wrote:Your solution was correct, it kept on reassigning the image for every new entity. I fixed this now, thanks ^^

how did you fix it?
42
User avatar
marvin
 
Posts: 49
Joined: Mon Oct 24, 2011 10:25 am

Re: Memory Leak

Postby Nishok » Thu Jun 07, 2012 2:30 am

marvin wrote:
Nishok wrote:Your solution was correct, it kept on reassigning the image for every new entity. I fixed this now, thanks ^^

how did you fix it?


I simply assigned one orc (no more arrays), and used the new function to create 200 more on my map.
Nishok
 
Posts: 49
Joined: Fri Oct 28, 2011 3:07 am


Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest

cron