Message Board


Message Board > C/C++ > Help me find memory leak!?

June 12, 2013, 07:31
DoctorN
Whiskered
91 posts
Download my source here
http://www.mediafire.com/downl … 1-2013_2314.zip
I have included all the .dll files you will need, the game uses SDL, SDL_image, and SDL_ttf and was compiled with Visual Studio 2008 (I also tried Dev-Cpp).

What problem am I having? Well, I am getting a memory leak from somewhere, for the longest time my game stayed at a constants 1mb of ram usage give or take. For some reason I see it jump up forever ;_;. I can't run it on the dreamcast/ps2 if its gonna do that, the system will run out of memory.

Q. Is the image/font being loaded over and over again?
A. No it is not, I even have it displayed to console when it loads them, shows up once.

Q. Is the game running *this command* over and over again?
A. I have no idea if that is what will cause a memory leak, it hasn't in the past, and it shouldn't be now.

Please, I know this forum is "dead", but all you people who reply DO deliver from everything I read. So hopefully one of you will take a look at it really soon. Thanks

P.S. If you can't help me with the memory leak, but you have a good idea about collision and variable jumping, can you help me with that instead? Go to player::air() and you will see I have yVel set to -yspeed when I go up, and set to yspeed when I want to go down. I had something going where if it was jumping and yVel was greater than -yspeed, it would reach there and vice versa for yVel being less than yspeed (creating that better "U" shaped jump, I also had it set so once it changed from jumping to falling, yVel became 0). And it did work, and the player would collide with the ground, however the player would be shown like hovering way above the ground and all that stuff.

Thanks
____________
#
June 13, 2013, 05:37
DoctorN
Whiskered
91 posts
nevermind, i solved the memory leak I had to free the sdl_surface constantly and thats what works
____________
#
June 13, 2013, 10:19
Zomg
None
641 posts
Yeah, I don't know anything about SDL. :o
____________
#
June 13, 2013, 21:43
DTM
Earthling!
821 posts

personally if I was doing anything 2D and C++ I'd use something like Box2D for physics.

I'm on linux and compiling it all would be a pain so it's hard to understand what the problem even is.

To get some entity to sit precisely on the ground, you need to check collisions for every pixel.

instead of

Code:
y += yVel;

shift_boxes();

if (check_collision)
  ...blah


have something like

Code:
yNew = y + yVel;

while ( y != yNew && yVel != 0 )
{
    if (yVel > 0)
        y ++;
    else
        y --;
   
    shift_boxes();
   
    // Check collisions at pixel level
    if( check_collision( box, rects ) )
       ...blah
}


(alternatively do as you do already, but then iterate upwards performing collisions until you find where exactly the ground is. maybe that is more efficient, because you do pixel level detection only once you hit something, but it's also then possible for you to fall through the ground when falling at high speed.)

[Edited on June 13, 2013 by DTM]
____________
:o
#
June 17, 2013, 18:58
DoctorN
Whiskered
91 posts
thanks, I am using this
Code:
 yNew = y + yVel;

    while ( y != yNew && yVel != 0 )
    {
        if (yVel > 0)
        {
            y ++;
        } else {
            y --;
        }
       
        shift_boxes();
       
        // Check collisions at pixel level
        if( check_collision( box, rects ) )
        {
            if( falling == true && ( on_ground && jumping ) == false )
            {
                on_ground = true;
                falling = false;
            }
            if( jumping == true && ( on_ground && falling ) == false )
            {
                yVel = 0;
                air_time = air_climax;
            }
            if( on_ground == true && ( jumping && falling ) == false )
            {
                y -= yVel;
            }
            shift_boxes();
        } else {
            if ( jumping == false )
            {
                falling = true;
                on_ground = false;
            }
        }
    }


so just replace the bit of code I uploaded with that (be sure to put "int yNew;" in player.h. What will happen is the game will start up, you will not be falling. You can walk left and right (collision is fine for that). Now you can jump and hit the cieling spot on, but once you land the game freezes up.

Can you take a look into that? Also, to compile the game on linux use the template:

Quote:
g++ -o myprogram mysource.cpp -lSDL -lSDL_image -lSDL_ttf


EDIT:

If you change a part of that code to this
Code:
 if (yVel >= 0)
        {
            y ++;
            cout << "y++;" << endl;
        } else {
            y --;
            cout << "y--;" << endl;
        }

You will see that as you jump, it says "y--;", and when falling it says "y++;". However, as you land on the ground then "y++;" is printed at an alarming, game crashing rate. I do not know whats going on.

EDIT 2:
also change this out
Code:
// Check collisions at pixel level
        if( check_collision( box, rects ) )
        {
            if( falling == true && ( on_ground && jumping ) == false )
            {
                cout << "collision with ground" << endl;
                on_ground = true;
                falling = false;
            }
            if( jumping == true && ( on_ground && falling ) == false )
            {
                cout << "collision with ceiling" << endl;
                yVel = 0;
                air_time = air_climax;
            }
            if( on_ground == true && ( jumping && falling ) == false )
            {
                cout << "still on the ground" << endl;
                y -= yVel;
            }
            shift_boxes();
        } else {
            if ( jumping == false )
            {
                cout << "nothing below you so falling" << endl;
                falling = true;
                on_ground = false;
            }
        } 


you will see when you land the game will freak out, printing about five "nothing below you so falling" and "y++;" to every "collision with ground".

[Edited on June 17, 2013 by DoctorN]
____________
#
June 18, 2013, 18:02
DTM
Earthling!
821 posts

OK, it's probably because you modify y or yVel in the inner statements, and the outer while loop requires specific conditions to finish... so there is some infinite loop somehow..

you want to break out the while loop if any collision occurs:

Code:
    while ( y != yNew && yVel != 0 )
    {
        if (yVel > 0)
        {
            y ++;
        } else {
            y --;
        }
       
        shift_boxes();
       
        // Check collisions at pixel level
        if( check_collision( box, rects ) )
        {
            if( falling == true && ( on_ground && jumping ) == false )
            {
                on_ground = true;
                falling = false;
            }
            if( jumping == true && ( on_ground && falling ) == false )
            {
                yVel = 0;
                air_time = air_climax;
            }
            if( on_ground == true && ( jumping && falling ) == false )
            {
                y -= yVel;
            }
            shift_boxes();

            break; // BREAK OUT HERE!
        } else {
            if ( jumping == false )
            {
                falling = true;
                on_ground = false;
            }
        }
    }


I'm not really comprehending your code as a whole though, since it's rather complicated. So there could be other problems.

I can't even get SDL to install here, so I can't test anything. my package manager is borked.

[Edited on June 18, 2013 by DTM]
____________
:o
#
June 19, 2013, 14:12
DoctorN
Whiskered
91 posts
do you have Fedora?
____________
#

Message Board > C/C++ > Help me find memory leak!?

Quick reply


You must log in or register to post.
Copyright © 2005 Booleansoup.com
Questions? Comments? Bug reports? Contact us!