0

I've got the following code in a thread in my application:

while (true) {
    if (ready) {
        progressIndicatorController.value++;
        return;
    }
}

The ready variable is changed from a delegate method. This code works great when I open the application by clicking the "Run" button in Xcode's toolbar. However, if I open this application's .app (which I create by clicking Product > Archive and then following the steps) this code somehow doesn't work anymore.

progressIndicatorController.value is never incremented and this if-statement never evaluates to true. What could cause this problem?

Frog
  • 1,631
  • 2
  • 17
  • 26

1 Answers1

1

This is probably caused by optimization from the compiler.

When you build with Archive, XCode enabled optimization in the compiler that could throw this kind of code away. I think setting the ready variable to volatile could fix your problem, altough if I were you I'd just try to rewrite it so it doesn't trigger this problem.

You can test with optimization turned on by choosing Edit Schemes in the scheme dropdown. Then set Build Configuration to Release in the Run MyApp.app. Don't forget to set it back to Debug when you're done though, as the debugger gets somewhat confused when optimization are on (i.e. you can't see the value of most variables, some breakpoints may behave erratically, etc...)

Taum
  • 2,511
  • 18
  • 18
  • Indeed, setting `ready` to `volatile` solved the problem. How should I rewrite this code? I honestly can't come up with an alternative. Do you have any pointers as to what I should do? Thanks you very much for the answer! – Frog Jan 04 '13 at 23:19
  • It's difficult without knowing more about your code but usually a NSThread can be sent a message from another thread with `performSelector:onThread:withObject:waitUntilDone:`. See http://stackoverflow.com/questions/2584394/iphone-how-to-use-performselectoronthreadwithobjectwaituntildone-method for an example. Also sometimes it's as easy as using two dispatch_async, one for your thread block and the other for when it's done! – Taum Jan 07 '13 at 14:20