4

When restarting my game, it crashes with low memory warnings. I am looking for a way to close the EAGLView and stoping all processes. I am not sure what to show you, so please ask for more info if need be.

I have an EAGLView with a mainGameLoop as followed.

- (void)mainGameLoop {

    // Create variables to hold the current time and calculated delta
    CFTimeInterval      time;
    float               delta;

    // This is the heart of the game loop and will keep on looping until it is told otherwise
    while(true) {

        // Create an autorelease pool which can be used within this tight loop.  This is a memory
        // leak when using NSString stringWithFormat in the renderScene method.  Adding a specific
        // autorelease pool stops the memory leak
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        // I found this trick on iDevGames.com.  The command below pumps events which take place
        // such as screen touches etc so they are handled and then runs our code.  This means
        // that we are always in sync with VBL rather than an NSTimer and VBL being out of sync
        while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.02, TRUE) == kCFRunLoopRunHandledSource);

        // Get the current time and calculate the delta between the lasttime and now
        // We multiply the delta by 1000 to give us milliseconds
        time = CFAbsoluteTimeGetCurrent();
        delta = (time - lastTime) * 1000;

        // Go and update the game logic and then render the scene
        [self updateScene:delta];
        [self renderScene];

        // Set the lasttime to the current time ready for the next pass
        lastTime = time;

        // Release the autorelease pool so that it is drained
        [pool release];
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Lohardt
  • 1,057
  • 1
  • 12
  • 26
  • First, you're going to need a way to break out of your game loop. Have you thought about having your `while()` condition actually check against a value? Also, using a `while()` loop might not be the best way to update your display. You really should look into using a CADisplayLink for that. Finally, [EAGLView isn't a stock Cocoa class](http://stackoverflow.com/a/8438138/19679), it's just a custom UIView that hosts an OpenGL ES CAEAGLLayer that's sometimes used in Apple's sample code, so some people might not know what that is. – Brad Larson Apr 20 '12 at 14:45
  • The example is the raw mainGameLoop. I thought someone might have aother way of doing it. Atm i am using while(self.hidden == FALSE) and hiding the view when displaying the scores etc. My game breaks at while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.02, TRUE) == kCFRunLoopRunHandledSource); right now, after replaying the game a few times. – Lohardt Apr 21 '12 at 08:59
  • 1
    Why not use the NSTimer or CADisplayLink instead ? – Max Apr 21 '12 at 13:47

1 Answers1

0

I have published a game, and I used an EAGLView. I also used a timer to control my Frames / Second (Said to also be a good way of controlling your power utilization on the device itself).

The best way is to recognize all instances when the game is being exited (even hitting the home button) and control every bit of your exitting routine. If implementing the timer above, you can stop the timer, dealloc stuff, etc...

On re-entry, you are given routines in the API that if you implement essentially give you the opportunity to know that you are being re-entered from an in-game state versus your app being completely re-started from scratch.

trumpetlicks
  • 7,033
  • 2
  • 19
  • 33