Swift code that works in main thread crashes when used in NSThread
| Originator: | kristopherdjohnson | ||
| Number: | rdar://19353741 | Date Originated: | 28-Dec-2014 09:25 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | 6.1.1 (6A2008a) |
| Classification: | Crash/Hang/Data Loss | Reproducible: | Always |
Summary: I have a command-line tool application that is written in Swift. When the code is run on the main thread, it works fine. However, when the same code is run in an NSThread, with the main thread doing nothing but waiting for the NSThread to exit, I get crashes. The most common crash is an EXC_BAD_ACCESS, but while making modifications to the code I sometimes get SIGABRT, sometimes I get warnings about method tables being corrupted, sometimes I get messages about memory checksums being incorrect. It seems that some kind of memory corruption is happening when the code is run on a thread other than the main thread. I've tried this same code in an iOS application, with the same results (crashes if run in NSThread), so I don't think the problem is related to OS X or a command-line tool. It is possible that my code has a memory-corruption bug that only manifests itself when run in another thread, but in my review of the code I don't see any unsafe use of pointers, memcpy, or anything else that might violate Swift's safety guarantees. Steps to Reproduce: An Xcode project is attached. Decompress and open the project. Note that there are two command-line tool targets: finchbasic and finchbasic_NSThread. The application is a simple BASIC interpreter. If you run the finchbasic target, it will display a ":" prompt and you can enter BASIC commands like these: 10 PRINT "Hello, world!" 20 END LIST RUN If, however, you run the finchbasic_NSThread target, it will crash. The only difference between finchbasic and finchbasic_NSThread is that the former runs the code on the main thread, and the latter runs it on an NSThread. See the file finchbasic/main.swift to see the differences. Expected Results: Code should run on NSThread just as it does on main thread Actual Results: Code crashes with apparent memory corruption issues Version: 6.1.1 (6A2008a) Notes: Configuration: OS X Attachments: 'FinchBasic_NSThread_Crash.zip' was successfully uploaded. (Attachment can be downloaded here: https://dl.dropboxusercontent.com/u/1437706/FinchBasic_NSThread_Crash.zip)
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!