Loop that works in debug but not release.

I have the following code in my app, which iterates over an array of objects to find the max value of one of the fields:


var high: Float = 0

let readings: [Reading] = test.readings


for i in 0 ... readings.count {

let load = readings[i].loadReading

if load != nil{

if load > high{

high = load!

}

}

}

This code works fine in debug mode, but invariably crashes in release mode. The values have all been used previously in the loop, and produce the correct effects. test.readings is optional, but the code will not progress to this loop if it is. Has anyone got any ideas about what may change between debug and release mode that will cause this?

Accepted Reply

Lots of compiler optimizations are enabled in release but not in debug mode because they require extra compile time, which gets pesky during debugging. And it turns out that a lot of compiler optimizations involve loops. I suspect that one of those optimizations is not working correctly. To verify this:

•Go to your app's build settings.

•Search for the "Optimization Level" build setting.

•Expand it if necessary and switch the release setting to None [-O0]

•Maybe also disable the optimization profile usage (search "Use Optimization Profile")

•Now build and run again in Release.


Hopefully the loop will work now. If it does, the problem must be in the optimizations, so file a bug at bugreport.apple.com.

  • Thank you for this!

Add a Comment

Replies

Lots of compiler optimizations are enabled in release but not in debug mode because they require extra compile time, which gets pesky during debugging. And it turns out that a lot of compiler optimizations involve loops. I suspect that one of those optimizations is not working correctly. To verify this:

•Go to your app's build settings.

•Search for the "Optimization Level" build setting.

•Expand it if necessary and switch the release setting to None [-O0]

•Maybe also disable the optimization profile usage (search "Use Optimization Profile")

•Now build and run again in Release.


Hopefully the loop will work now. If it does, the problem must be in the optimizations, so file a bug at bugreport.apple.com.

  • Thank you for this!

Add a Comment

I'm not sure if it's a typo introduced while editing your code for the forums (because I would assume it would crash your debug build too), but your range is bad and should be

0 ..< readings.count



I don't think it would be causing the crash (and could be another typo), but when you check if load > high it is calling the T?>T? overload of > because load is an optional, even though high is not.


You could also simplify your code there with 'if let' to avoid having to worry about unwrapping:

for i in 0 ..< readings.count
{
    if let load = readings[i].loadReading
    {
        if load > high {high = load}
    }
}

Thankyou for your reply. I disabled, the optimisations and it worked. I am going to investigate the code closely, just to make sure that there isn't another bug that fed into the optimisation issue. The code is working now though. Thankyou.

That was a typo from copying over. I had also tried a "for x in array" style loop, and using a map as well. Thankyou for the suggestion though 🙂

Hi,


I updated my xcode 9.4 to 10.1 (updated swift too) and I got the same issue with while loop.


I want to release the app to the store. Will my app crash if I will keep run mode as debug mode and archive the release build?

Or do I must have to change the "Optimization Level" build setting to None [-O0] before releasing it.


I am little confused. Please guide.


Thanks,

Bhavya Bharti