Uploaded image for project: 'Swift'
  1. Swift
  2. SR-4497

Memory issue causes crash when application built with optimization turned on.

    XMLWordPrintable

    Details

      Description

      I've noticed crashes after I upgraded Swift to version 3.1. It happens only when optimization is set to "-O" or "-O whole-module-optimization"
      Here is an example code (Example app crashes when following method is called a few times (usually two is enough).

      fileprivate func funWithWords(_ words: [String]) -> [String] {        
              var resultArray = [String]()
              
              for index in 0..<words.count {
                  // This will crash
                  let pre = words[0...index].reduce("", +)
                  resultArray.append(pre)
                  
                  // This version is ok
      //            let pre = words[0...index].reduce("", +)
      //            resultArray = resultArray + [pre]
                  
                  // This version is ok too
      //            let pre = words[0...index].joined()
      //            resultArray.append(pre)
                  
                  // This version is ok too
      //            let pre = words[0...index]
      //            let reduced = pre.reduce("", +)
      //            resultArray.append(reduced)
              }
              return resultArray
      }
      

      Instruments also detects leak in that code. I am attaching screenshot of stack trace from Instruments and Xcode.
      I will verify if issue exists on swift-master as well.

        Attachments

        1. OptimizationExample.zip
          30 kB
        2. screen1.png
          screen1.png
          80 kB
        3. screen2.png
          screen2.png
          175 kB

          Issue Links

            Activity

              People

              Assignee:
              eeckstein Erik Eckstein
              Reporter:
              anaglik Andrzej Naglik
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: