xboxscene.org forums

Author Topic: Would Someone Be Able To Look Through Code For Me?  (Read 91 times)

Smoko

  • Archived User
  • Jr. Member
  • *
  • Posts: 73
Would Someone Be Able To Look Through Code For Me?
« on: October 16, 2005, 03:47:00 AM »

I'm not to good with GUI stuff, but have you checked for any possible memory leaks or Infiniate loops?
Logged

Ecrofirt

  • Archived User
  • Full Member
  • *
  • Posts: 166
Would Someone Be Able To Look Through Code For Me?
« Reply #1 on: October 20, 2005, 10:06:00 PM »

Wondering if anyone might be able to please look through this for me.

I've been hung up on whatever this bug is for nearly a year, and it's stopping me from making any progress in the game.
Logged

XBOX War3z

  • Archived User
  • Hero Member
  • *
  • Posts: 587
Would Someone Be Able To Look Through Code For Me?
« Reply #2 on: October 21, 2005, 05:04:00 AM »

It would have been easier to find the bug if you could run it on win smile.gif
Ok I checked up my old HCE sources and found these methods:

CODE
   MEMORYSTATUS stat;
   GlobalMemoryStatus( &stat );


You can use this to see if there is a memory leak. Don't have a modded xbox so can't test the code.

Btw, the method is a normal Xbox XDK method so you can find information about it in the Xbox XDK Documentation.

CODE
//------------------------------------------------------------------------------
// globstat.cpp
//
// Example use of GlobalMemoryStatus()
//
// Sample output:
//
// 2047 total MB of virtual memory.
// 2046  free MB of virtual memory.
//   64 total MB of physical memory.
//   63  free MB of physical memory.
//    0 total MB of paging file.
//    0  free MB of paging file.
//    0  percent of memory is in use.
//------------------------------------------------------------------------------
#include

// 1 megabyte
#define MB   (1024*1024)

// Add one line of text to the output buffer.
#define AddStr(a,b) (pstrOut += wsprintf( pstrOut, a, b ))

void main( int argc, char *argv[] )
{
    MEMORYSTATUS stat;
    CHAR strOut[1024], *pstrOut;

    // Get the memory status.
    GlobalMemoryStatus( &stat );

    // Setup the output string.
    pstrOut = strOut;
    AddStr( "%4d total MB of virtual memory.\n", stat.dwTotalVirtual / MB );
    AddStr( "%4d  free MB of virtual memory.\n", stat.dwAvailVirtual / MB );
    AddStr( "%4d total MB of physical memory.\n", stat.dwTotalPhys / MB );
    AddStr( "%4d  free MB of physical memory.\n", stat.dwAvailPhys / MB );
    AddStr( "%4d total MB of paging file.\n", stat.dwTotalPageFile / MB );
    AddStr( "%4d  free MB of paging file.\n", stat.dwAvailPageFile / MB );
    AddStr( "%4d  percent of memory is in use.\n", stat.dwMemoryLoad );

    // Output the string.
    OutputDebugString( strOut );

    // Don't return from main!
    Sleep( INFINITE );
}
Logged

Ecrofirt

  • Archived User
  • Full Member
  • *
  • Posts: 166
Would Someone Be Able To Look Through Code For Me?
« Reply #3 on: October 21, 2005, 06:21:00 PM »

fucking hell, something is chewing through a meg of ram every two seconds.

I've gone to the point where I've commented everything out except my update_gfx function.

At this point, there should be no variables being made or anything, and yet, I'm still running through a meg of memory every two seconds

I have NO IDEA what could be causing this at all.
Logged

XBOX War3z

  • Archived User
  • Hero Member
  • *
  • Posts: 587
Would Someone Be Able To Look Through Code For Me?
« Reply #4 on: October 22, 2005, 11:30:00 AM »

So you mean that in main.cpp you changed main to do this in the while?

CODE
   while(!done) {
   
  frame++;         //count another frame

  update_gfx();      //blits everything to the screen

  //this is for displaying the fps
  NowTime=SDL_GetTicks();    //update the current time

  //check to see if a second has passed since the start of the time counting
  //if it has, we'll update the fps number for blitting on the screen
  if (NowTime>(StartTime+1000)) {   
     StartTime=SDL_GetTicks();  //reset the start time
     fps=frame;      //set the current fps to whatever our frame is at now
     frame=0;      //reset our frame count to zero
  }
   }


What is the FPS on that? Does it leak faster or slower then using all the normal code?
Logged

d0wnlab

  • Archived User
  • Sr. Member
  • *
  • Posts: 326
Would Someone Be Able To Look Through Code For Me?
« Reply #5 on: October 22, 2005, 12:17:00 PM »

I see at least one, off the bat.  It's too bad though, because it's due to your memory checking code smile.gif

CODE

  textSur=TTF_RenderText_Solid(text, strOut , fontcolor);


That allocates space for an SDL_Surface.  Where do you

CODE

SDL_FreeSurface(textSur);


Also make sure that you are freeing every sdl_surface you create.  I wasn't in my dash for one of my surfaces that I was creating every time the user moved in a menu - about a meg a second when they would move.
Logged

d0wnlab

  • Archived User
  • Sr. Member
  • *
  • Posts: 326
Would Someone Be Able To Look Through Code For Me?
« Reply #6 on: October 22, 2005, 02:42:00 PM »

Any time that you create a surface (rendering text, or SDL_CreateRGBSurface are the two ways you'll prolly see often) it should be deleted at some point.  Now, for the game screen/buffer, and the surfaces for each of the animation frames of all the baddies/etc, you only delete them when you're done with them (which normally would be like, when quitting the game).  You really don't have to care too much about that since it doesn't cause a periodic memory leak.  However, for surfaces that are created for every iteration of you game loop, you better as hell delete them before you create the next one or else you are gobbling up memory every time.  SO, in update_gfx, after flipping the screen, do a

SDL_FreeSurface(textSur);

Then see if there is still a leak.

Tom
Logged

d0wnlab

  • Archived User
  • Sr. Member
  • *
  • Posts: 326
Would Someone Be Able To Look Through Code For Me?
« Reply #7 on: October 22, 2005, 03:50:00 PM »

That looks right.

Take a look in your code.  Find every usage of "new".  Find it's corresponding delete.  Ensure that every time the "new" is used, make sure the "delete" is used before that "new" could occur again.

Another thing that could help is, look at the exact size of memory that is being leaked in each instance.  It sometimes will give an indication to what it is (ie, if it's always 44 bytes, try to find a data structure you use that is 44 bytes long).
Logged

Ecrofirt

  • Archived User
  • Full Member
  • *
  • Posts: 166
Would Someone Be Able To Look Through Code For Me?
« Reply #8 on: October 22, 2005, 04:25:00 PM »

Here's how I've been using new:
         ENEMY *NewEnemy;                           
CODE
//this creates a new enemy
     
     //this chooses a random number based on the current level.
     //It then creates an enemy based on that number, and puts
     //it into the enemy list.
     //NOTE: I've currently just got it randomly generating one of the available enemies.
     switch (rand()%4) {
    case 0:
       NewEnemy=new Pig(level);
       OutputDebugString("pig created.\n");
       break;
    case 1:
       NewEnemy=new Skeleton(level);
       OutputDebugString("skeleton created.\n");
       break;
    case 2:
       NewEnemy=new Zombie(level);
       OutputDebugString("zombie created.\n");
       break;
    case 3:
       NewEnemy=new Goblin(level);
       OutputDebugString("goblin created.\n");
       break;
     }

     vEnemy.push_back(NewEnemy);         //this pushes the enemy back


Funnily enough, this is how I was directed to make the enemies when I switched over to a list from a vector.

Am I doing it wrong?
Logged

d0wnlab

  • Archived User
  • Sr. Member
  • *
  • Posts: 326
Would Someone Be Able To Look Through Code For Me?
« Reply #9 on: October 22, 2005, 05:10:00 PM »

That should be fine.  Do any of the pig/goblin/etc.. classes contain data members that are allocated usuing new in their constructors?  If so, make sure to delete that data as well.
Logged

d0wnlab

  • Archived User
  • Sr. Member
  • *
  • Posts: 326
Would Someone Be Able To Look Through Code For Me?
« Reply #10 on: October 22, 2005, 06:02:00 PM »

right. except.. when it is time for an enemy to be deleted, could he still have a projectile that is his?  If so, you have to delete that projectile before you delete the enemy, otherwise you're leaking memory.
Logged

d0wnlab

  • Archived User
  • Sr. Member
  • *
  • Posts: 326
Would Someone Be Able To Look Through Code For Me?
« Reply #11 on: October 22, 2005, 06:52:00 PM »

I see that now.  The data being initialized is remembered by the game not by each individual enemy object.  That's not a problem then.
Logged