EXC_BAD_ACCESS with optimization==Fast

hej,

our ios app runs fine (with xcode 7) in the simulator and on the device without optimization. with enabled optimization the app starts, but on some user input the app crashs with the message:

"Thread 1: EXC_BAD_ACCESS (code=1, address=0x....)"

it looks that some parts of the model (global variable) doesn't exist anymore. it crashs in the funktion:

func getUnknownString () -> String {
    return model.unknownStack.peek ()!
}


po model.unknownStack returns:

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0xe000000013896413).
The process has been returned to the state before expression evaluation.


po model:


0x000000013766e720
{
  unknownIsForced = <invalid> (0x5)
  unknownStack = 0x000000013775d7d0 {
    size = 2
    items = {}
  }
...
...
and many
<read memory from 0x... failed (0 of ... bytes read)>


unknownStack is a normal stack implemented with generics.


optimization worked fine(*) on xcode 6.

i hope you can help.

p



* with swift 1.2 / xcode6 we had some strange problems but we could fix everything with refactoring some methodes.

Similar issue here on tvOS 9.1 GM Swift app: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) with either Fast or Whole Module optimization levels. Runs fine with None. Also occurs on iOS 9.1 GM.


Here's the smallest example that exhibits the issue. Radar 23226145 filed.


import UIKit

enum Rank: Int {
    case Ace = 1
    case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
    case Jack, Queen, King
    static let allValues = [Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King]
}

enum Suit: Int {
    case Spades = 0, Hearts, Diamonds, Clubs, Cut
    static let allValues = [Spades, Hearts, Diamonds, Clubs]
}

class Card {
    var rank: Rank
    var suit: Suit
    var shuffleIndex: UInt32 = 0

    init(rank: Rank, suit: Suit) {
        self.rank = rank
        self.suit = suit
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
     
        var shoe: [Card] = []
     
        for rank in Rank.allValues {
            for suit in Suit.allValues {
                let card = Card(rank: rank, suit: suit)
                card.shuffleIndex = arc4random_uniform(UINT32_MAX)
                shoe.append(card)
            }
        }

        // EXC_BAD_ACCESS on this line with optimization other than None
        shoe.sortInPlace { $0.shuffleIndex < $1.shuffleIndex }
     
        for card in shoe {
            print("\(card.rank)  \(card.suit)  \(card.shuffleIndex)")
        }
    }
}
EXC_BAD_ACCESS with optimization==Fast
 
 
Q