Xcode-Beta-6.3 (6D520o): Swift: Crash when using String.lowercaseString in an autorelease pool

Originator:npankey
Number:rdar://19865083 Date Originated:17-Feb-2015 01:36 AM
Status:Resolved Resolved:23-Feb-2015
Product:Developer Tools Product Version:Xcode-Beta-6.3 (6D520o)
Classification:Crash Reproducible:Always
 
Summary:
In Swift calling a method/property on __FUNCTION__ (e.g. lowercaseString) from inside an autorelease pool crashes when exiting the pool due to an over-release.

Steps to Reproduce:
Paste this code into main.swift for a new command line app and run it

import Foundation

func crashOnRelease() {
    println(__FUNCTION__.lowercaseString)
}

autoreleasepool {
    crashOnRelease()
}


Expected Results:
Generated executable prints "crashonrelease()" and exits cleanly

Actual Results:
Generated executable prints "crashonrelease()" but crashes when exiting the autoreleasepool with the following stack:

(lldb) bt
* thread #1: tid = 0x76fdb, 0x00007fff9683e44f libobjc.A.dylib`objc_release + 15, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x20)
    frame #0: 0x00007fff9683e44f libobjc.A.dylib`objc_release + 15
    frame #1: 0x00007fff9683ce8f libobjc.A.dylib`(anonymous namespace)::AutoreleasePoolPage::pop(void*) + 575
    frame #2: 0x000000010000685e modifyfunctionname`ObjectiveC.autoreleasepool (() -> ()) -> () + 46
  * frame #3: 0x00000001001bf477 modifyfunctionname`main + 119 at main.swift:17
    frame #4: 0x00007fff917a65c9 libdyld.dylib`start + 1


Version:
Xcode-Beta-6.3 (6D520o)

Notes:
Discovered when creating `XCTestCase`s that use __FUNCTION__ since those are executed inside an autorelease pool.

This doesn't crash in Xcode 6.1.1 (6A2008a)

Configuration:
Always on Xcode-Beta-6.3 on Yosemete

Alternative repro:
This problem isn't limited to the __FUNCTION__ string. I've found that reading the contents of a file into a string and then using `lowercaseString` inside an autorelease pool will similarly crash due to an over-release.

import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        autoreleasepool {
            let bundle = NSBundle(forClass: self.dynamicType)
            if let path = bundle.pathForResource("Hello", ofType: "txt"),
                var text = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil) {
                    text.lowercaseString
            }
        }
    }
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!