Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with infinite loops in Objective C

I've recently joined an iPad project. In looking through the code base I've come across some unusual things, one of which is this (I've meta-coded this):

while (something.isAlwaysTrue) {
    // Wait for something to happen. i.e. Get data from an internet connection.
    // Respond to something.
}

I didn't notice any problems with this until I started memory profiling the app and found that these loops are memory leaking massively. The reason being that because they never end, any autorelease instances created inside them are never freed because the method never ends and the autorelease pools never get a chance to free up.

After talking it through with the developers who wrote the code I came up with the following technique:

-(void) queueTask {
    // Using GCD or perform with delay, call the process method.
}

-(void) process {

    // Wait and/or do stuff. 

    [self queueTask];
}

The basic idea is that by using a method to queuing through GCD or the runloop, it gives the autorelease pool a chance to execute and clean up autorelease instances. This appears to work just fine.

My question is - is this the best way to go about dealing with these loops? or is there a better way to do it?

like image 242
drekka Avatar asked Oct 22 '11 05:10

drekka


People also ask

How do you deal with an infinite loop?

To avoid ending up in an infinite loop while using a for statement, ensure that the statements in the for() block never change the value of the loop counter variable. If they do, then your loop may either terminate prematurely or it may end up in an infinite loop.

How do you break out of a loop in Objective C?

'break' will only get you out of the innermost loop or switch. You can use 'return' to exit out of a function at any time.

How do I stop a while loop running infinitely?

When you write a while loop, you need to make the necessary updates in your code to make sure that the loop will eventually stop. An infinite loop is a loop that runs indefinitely and it only stops with external intervention or when a break statement is found. You can stop an infinite loop with CTRL + C .


1 Answers

Two points;

Minimizing Heap Growth

anyway, here's how minimize memory growth:

while (something.isAlwaysTrue) {
  NSAutoreleasePool * pool = [NSAutoreleasePool new];
  // Wait for something to happen. i.e. Get data from an internet connection.
  // Respond to something.
  [pool release], pool = 0;
}

or if you prefer the bleating (sic) edge:

while (something.isAlwaysTrue) {
  @autoreleasepool{
    // Wait for something to happen. i.e. Get data from an internet connection.
    // Respond to something.
  }
}

autorelease pools just operate like thread local stacks. when you push a pool, autoreleased objects are added to the top pool in the current thread. when you pop the pool, the pool sends a release message for each autorelease.

using GCD as a substitute for an autorelease pool is odd; similar to using an NSArray of single character NSStrings where you should simply use a single NSString.

Mutithreaded Program Flow

the infinite loop is a very suspicious program. it suggest you may be trying to reinvent run loops. the main run loop is of course common. a secondary thread 1) with a run loop 2) that never ends is unusual.

you should reconsider how the program flows. typically, you act on events, rather than holding your breath, polling until they complete. you may be trying to break from that situation in the program you proposed -- but i don't have enough detail.

like image 144
justin Avatar answered Oct 03 '22 00:10

justin