Crash on swift class init when using inline initializers - swift

I'm developing an application and I encountered a crash that I can't explain.
The library is fairly complex so I prepared a minimum example
struct Info {
static let test = 1
}
class SuperCls<A> {}
class Cls<A>: SuperCls<A> {
let v: Info.Type = Info.self
}
let v = Cls<Int>()
This code crashes when I alloc the class, in the very last line, with the following error
file:///play.playground/: error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x4403).
I also tried this in an xcode project and the error is pretty much the same
I found a way to make this code works
struct Info {
static let test = 1
}
class SuperCls<A> {}
class Cls<A>: SuperCls<A> {
var v: Info.Type?
override init() {
v = Info.self
super.init()
}
}
let v = Cls<Int>()
Can someone tell me why this crash occurs?
Thanks!
UPDATE
I also discovered that this works
struct Info {
let test = 1
}
class SuperCls<A> {}
class Cls<A>: SuperCls<A> {
let v = Info()
}
let v = Cls<Int>()
The problem seems to be related to the fact that I'm using Info as a type and accessing static information

Alright, it seems that it is a bug in Xcode beta 5
Thanks for the help

Related

NSXPCInterface setClasses causes "Lazily named class 0x600000xxxxxx wasn’t named by lazy name handler" on Xcode 14+ Swift #objc enum

After updating to Xcode 14 I am getting the SIGABRT crash "Lazily named class 0x600000dc6520 wasn’t named by lazy name handler". In the latest version of Xcode 13 it compiles and runs without any flaw. The only thing different from the very basic is that I use the MyAppSharedObjects to get across more complex objects between the XPC Service and the main app. Apart from that it doesn't look like anything beyond tutorial level NSXPCInterface code to me:
import MyAppScriptSandbox
import MyAppSharedObjects
import Foundation
class ScriptExecutor {
init?() {
let incomingClasses = NSSet(array: [
NSArray.self,
NSString.self,
NSValue.self,
NSNumber.self,
NSData.self,
NSDate.self,
NSNull.self,
NSURL.self,
NSUUID.self,
NSError.self,
NSDictionary.self,
ScriptSandboxReply.self,
AppleScriptError.self,
AppleScriptErrorType.self
]) as Set
let remoteInterface = NSXPCInterface(with: MyAppScriptSandboxProtocol.self)
remoteInterface.setClasses( // **** CRASH HAPPENS HERE ****
incomingClasses,
for: #selector(MyAppScriptSandboxProtocol.execute(script:withReply:)),
argumentIndex: 0,
ofReply: true
)
import Foundation
import MyAppSharedObjects
#objc
public protocol MyAppScriptSandboxProtocol {
func execute(
script: String,
withReply reply: #escaping (ScriptSandboxReply) -> Void
)
func terminate()
}
#objc(ScriptSandboxReply)
public class ScriptSandboxReply: NSObject, NSSecureCoding {
public static let supportsSecureCoding = true
public func encode(with coder: NSCoder) {
// Removed company specific code
}
required public init?(coder: NSCoder) {
// Removed company specific code
}
}
This data type was the issue:
#objc(AppleScriptErrorType)
public enum AppleScriptErrorType: Int {
case error
case noResult
case errorNorResult // This really shouldn't happen IRL
static let key = "AppleScriptErrorType"
}
After a bit of dabbing I found the issue. I started to comment out custom data types in the incomingClasses list and while the application crashed upon the first received message it stopped crashing on init. By uncommenting the custom data types one by one I finally found the culprit:
#objc(AppleScriptErrorType)
public enum AppleScriptErrorType: Int {
case error
case noResult
case errorNorResult // This really shouldn't happen IRL
static let key = "AppleScriptErrorType"
}
I have no idea why this perfectly sane enum type could not be transferred anymore after Xcode 14 dropped while it worked in Xcode 13.4.1, I think it might be an Apple bug. However I could get on with my life using the rawValue (so just the int value really) to send the value from one side and to reconstruct the AppleScriptErrorType enum on the receiving side.

What does `outlined init with copy of protocol` mean?

I wrote very simple piece of code in Swift:
protocol MultiplyByTwoProtocol {
func multiply() -> Int
}
class MultiplyByTwoClass: MultiplyByTwoProtocol {
private let n: Int
init(n: Int) { self.n = n }
func multiply() -> Int { return 2 * n }
}
class DynamicDispatchSwift {
private let items: [MultiplyByTwoProtocol]
init(n: Int) {
self.items = Array<Int>.generate(size: n).map(MultiplyByTwoClass.init)
}
func run() {
items.forEach { input in
_ = input.multiply()
}
}
}
(btw generate method for Array just creates an array of random Ints)
Then, I run that code in Instruments and I got following results:
As you can see, almost half of the time takes entry called outlined init with copy of MultiplyByTwoProtocol. Does anyone know what it is?
Thanks for your help.
I just ran into outlined init with copy of ... as the top of a stack trace for crashes coming from an app built with release configuration. I found that turning off the compiler optimization settings would prevent the crash. Ultimately I also found an equivalent syntax for the code the stack trace pointed to, that the optimizer was having problems with, that did not cause crashes and was able to turn optimization back on.
In my case the line of code causing the issue was accessing a static constant inside a for loop. Assigning that same static constant to nothing just prior to the loop fixed the crash, as in this example:
let _ = Foo.fooConstant // This line fixed the crash
for index in 0..<values.count {
let someBar = Bar(foo: .fooConstant)
...
}

Swift 4 error on Release configuration

If you run this code on Xcode 9.1 using the Optimization level Fast, whole Module Optimization, it crashes. If the Optimization level is set to None, everything goes fine.
Does anyone have an idea of the issue?
protocol FooProtocol {
func foo()
}
class FooProtocolImplementation : NSObject, FooProtocol {
func foo() {}
}
var set: Set<AnyHashable> = []
_ = set.insert(FooProtocolImplementation())
let array = set.flatMap { $0 as? FooProtocol } // filtering out nils
_ = array[0] // # error (EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
Apple reported it as a bug and fixed it in Xcode 9.3

Protocol adoption by extension causing "Abort trap: 6" on Xcode 8.3.2/Swift 3.1

The following code causes the Swift compiler to crash with Command failed due to signal: Abort trap 6:
import UIKit
protocol ZeroProtocol {
static var zero: Self { get }
}
// CGPoint already has CGPoint.zero so we attempt to adopt ZeroProtocol.
// This causes "Abort trap: 6" on compilation.
extension CGPoint: ZeroProtocol {}
I'm almost certain that this code compiled a few months ago without trouble. Strangely, adopting the protocol with my own struct doesn't produce any error:
import UIKit
protocol ZeroProtocol {
static var zero: Self { get }
}
struct TestStruct {
static var zero = TestStruct()
}
// Works fine.
extension TestStruct: ZeroProtocol {}
I have submitted a bug report to Apple, but any workaround would be appreciated.

Error when using Generic as property type in Swift

I'm having an issue when using a Generic as the type constraint on a property. Here is a very simple example:
import UIKit
class TSSignal<MessageType> {
var message: MessageType?
init() {
}
}
In Xcode 6 Beta (6A215l) this will not compile. It fails with the following error at the bottom:
TSSignal.swift:13:9: error: unimplemented IR generation feature non-fixed class layout var message: MessageType? ^ LLVM ERROR: unimplemented IRGen feature! non-fixed class layout Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolc‌​hain/usr/bin/swift failed with exit code
But, if I remove the line var message: MessageType? it will build fine. Any ideas? Thanks.
Edit - changed code and error to reflect current status of issue
Edit - related: Swift compile error when subclassing NSObject and using generics
Update (6/18/14) - The issue still persists as of Xcode 6 - Beta 2
Update (7/25/14) - The issue still persists as of Xcode 6 - Beta 4 (thanks #Ralfonso, and I verified as well)
Update (8/4/14) - The issue is FIXED as of Xcode 6 - Beta 5!
There is a workaround without type erasure (works as of Xcode6-Beta2):
import UIKit
class TSSignal<MessageType> {
var _message: [MessageType] = []
func getMessage() -> MessageType? {
if _message.count > 0 {
return _message[0]
} else {
return nil
}
}
func setMessage(maybeMessage: MessageType?) {
if let message = maybeMessage {
_message = [message]
} else {
_message = []
}
}
init() {
}
}
EDIT EDIT:
This is definitely a bug in the compiler.
I tried to 'outsmart' the compiler by using the following:
class TSSignal<TMessage>
{
var messageType : Optional<TMessage> = nil
init() { }
}
Same issue.
This is NOT an "answer" (it is my own question), but I thought it worth noting what I did in this case to move passed this situation, for the time being.
import UIKit
class TSSignal {
var message: AnyObject?
init() {
}
}
Lame, but I am sure it's only temporary.