aleclarson
aleclarson

Reputation: 19035

Swift compiler segmentation fault when building

Adding a (convenient) computed height property to UIView in my UIViewExtension.swift file is causing the Swift compiler to segfault... What could possibly be going wrong here?

0  swift                    0x00000001061e5608 llvm::sys::PrintStackTrace(__sFILE*) + 40
1  swift                    0x00000001061e5af4 SignalHandler(int) + 452
2  libsystem_platform.dylib 0x00007fff894da5aa _sigtramp + 26
3  libsystem_platform.dylib 0xb03939841e997c88 _sigtramp + 2504775416
4  swift                    0x00000001064c8bb9 swift::NominalTypeDecl::getMembers(bool) const + 41
5  swift                    0x00000001055efab9 swift::irgen::ClassMetadataLayout<(anonymous namespace)::FindClassMethodIndex>::addClassMembers(swift::ClassDecl*) + 329
6  swift                    0x00000001055e97b2 swift::irgen::emitVirtualMethodValue(swift::irgen::IRGenFunction&, llvm::Value*, swift::SILType, swift::SILDeclRef, swift::CanTypeWrapper<swift::SILFunctionType>, swift::ResilienceExpansion) + 434
7  swift                    0x00000001056550d3 swift::SILVisitor<(anonymous namespace)::IRGenSILFunction, void>::visit(swift::ValueBase*) + 42611
8  swift                    0x000000010564a266 swift::irgen::IRGenModule::emitSILFunction(swift::SILFunction*) + 8678
9  swift                    0x00000001055cb6f8 swift::irgen::IRGenModule::emitGlobalTopLevel() + 184
10 swift                    0x00000001056376e3 performIRGeneration(swift::IRGenOptions&, swift::Module*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1859
11 swift                    0x0000000105638033 swift::performIRGeneration(swift::IRGenOptions&, swift::SourceFile&, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, unsigned int) + 51
12 swift                    0x00000001055aa65a frontend_main(llvm::ArrayRef<char const*>, char const*, void*) + 4842
13 swift                    0x00000001055a935d main + 1533
14 libdyld.dylib            0x00007fff8a82e5fd start + 1

 

1.  While emitting IR SIL function @_TFCSo6UIViewg6heightSd for 'anonname=0x7ff422892fd0' at <path redacted>/UIViewExtension.swift:60:5
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

If more information is needed to crack this, just comment. Thanks!

Edit:

Here's a related .xcodeproj that returns this question's compiler error. Download here

Upvotes: 104

Views: 63059

Answers (30)

Amir.n3t
Amir.n3t

Reputation: 3439

Segmentation faults can happen because of many reasons including compiler issues. I noticed in most cases using of protocols are involved and causes confusion in the compiler or sometimes module optimization process during compiler will lead to such an error.

I contacted Apple for one of these cases and they gave me a hidden attribute @_optimize(none) that you can add it to the initializer of your class to tell the compiler ignore the class from module optimization. I suggest give it a try if other methods suggested on the thread did not work for you.

@_optimize(none)
override init() {
    
    super.init()
}

Upvotes: 0

Anup Kulkarni
Anup Kulkarni

Reputation: 1

Follow this process:

  1. Update pods of existing project
  2. Go to edit and select convert > current swift syntax
  3. In build setting enable bitcode to "NO"
  4. Add run the project

Upvotes: 0

Marwen Doukh
Marwen Doukh

Reputation: 2050

I have had this error because my class does not conform to Equatable

Upvotes: 0

Helam
Helam

Reputation: 1505

Just got this error because I was using a feature of Swift 5.1 in a target that was not setup for Swift 5.1. Specifically, I was using Self. to access a static function instead of ClassName. which works in Swift 5.1 but trying to use it in an older target caused this same error.

Upvotes: 0

Maxim Smirnov
Maxim Smirnov

Reputation: 33

I was moving from Xcode 9 to 10.2. And here's a problem place

struct AbstractField<T>: ValidatableField {
    var name: String
    var value: Observable<Validated<T>>
    var setValue: (Validated<T>) -> Void
    var hint: Variable<String?>
    var error: Variable<AviaFieldError?>
    var isHidden: Binder<Bool>
    var isBeingEdited: Observable<Bool>
    var silentMode: Variable<Bool>
}

To solve a problem I replaced var to let for setValue closure.

struct AbstractField<T>: ValidatableField {
    var name: String
    var value: Observable<Validated<T>>
    let setValue: (Validated<T>) -> Void
    var hint: Variable<String?>
    var error: Variable<AviaFieldError?>
    var isHidden: Binder<Bool>
    var isBeingEdited: Observable<Bool>
    var silentMode: Variable<Bool>
}

Seems it was very hard to parse this construction for the compiler.

Upvotes: 0

Ned
Ned

Reputation: 1378

In my case the issue was in making extension with UITextFieldDelegate protocol to my class:

extension LoginPresenter: UITextFieldDelegate {}

To solve a problem I moved it to a class definition:

class LoginPresenter<T: LoginInteractionProtocol>: BasePresenter<T>, LoginPresenterProtocol, UITextFieldDelegate {
}

Upvotes: 0

devios1
devios1

Reputation: 38005

In my case the culprit was accidentally overloading a function expecting an array argument with one with a variadic argument:

public required init(_ args: Node...) {
}

When the superclass had it defined as an array:

public required init(_ args: [Node]) {
}

Upvotes: 1

UKDataGeek
UKDataGeek

Reputation: 6882

Apple should fix this in Xcode to give a better experience to us all. I recommend you log a bug with apple here: https://bugreport.apple.com and reference bug: 32707221 so they see how many people its affecting.

For me it was unwrapping 2 objects out of order

eg:

if let passengers = car?.passengers, 
let car = car {
} 

instead of

if let car = car,
let passengers = car.passengers
 {
}

hard to find!

Upvotes: 0

Beninho85
Beninho85

Reputation: 3311

You can also have this problem if you declare a condition with an unwrapped Bool as a property

Upvotes: 2

Alok C
Alok C

Reputation: 2857

I am working with xcode 7.3. Till after noon it was fine. suddenly I started seeing Seg Fault 11 for Objective C style selector. I don't know know what changed as xcode flipped on me (Of course I didn't update anything). It was literally just next run and it start freaking out. But I am able to get my project compile after adding #selector in one particular file for which xcode was complaining about.

Weird part is that It's not complaining about other files. It targeted only one file.

My recommendation see what file xcode is complaining about then work on all the warnings that its throwing for swift 3. You might be able to pass this seg fault.

Still at this moment reasons unknown. As I am able to clone the same project else where and compile just fine.

Upvotes: 0

Eugene P
Eugene P

Reputation: 572

Xcode 8.2.

Adding @nonobjc protocol implementation into extension causing segmentation faults. Move @nonobjc protocol implementation into class implementation.

Upvotes: 1

Petter
Petter

Reputation: 3021

Swift 3.0 (Xcode 8.1) exhibits this issue when a protocol declares an optional variable, and an implementer implements that variable as a lazy initialised one.

Bug is reported here: https://bugs.swift.org/browse/SR-1825

Upvotes: 1

Asaf Shveki
Asaf Shveki

Reputation: 736

I solved it by removing the Build Settings configuration "Objective-C Bridging Header" string I've added manually.

Upvotes: 0

Chris Prince
Chris Prince

Reputation: 7574

Yet another crash issue:

Variations of the following caused Xcode 8.2b2 compiler crashes in three places in my code:

return self.initialValue.mutableCopy() as AnyObject!

self.initialValue is defined as:

fileprivate  let initialValue:AnyObject!

I created various fixes for this, dependent on the context in my code. Here's one of the fixes:

let initialSetValue = self.initialValue as! NSMutableSet
let copy = NSMutableSet(set: initialSetValue);
return copy

Upvotes: 0

Bill
Bill

Reputation: 45418

When you run into a compiler segfault in Swift, you don't get a handy line number and error message. Here's how you can track the problem down:

  1. Create a new file called SegFaultDebugger.swift in your project.
  2. In this new file, define an extension to the class that's giving you problems.
  3. Move a group of methods from the main file to SegFaultDebugger.swift.
  4. Compile.

At this point, one of three things happens:

  • You still get the segfault in the original file: Move the methods from SegFaultDebugger.swift back to the original file and move a different set of methods into SegFaultDebugger.swift. Repeat
  • You get a segfault in SegFaultDebugger.swift: Great! Now use binary search to pin the segfault down to a specific method until you can figure out what construct is causing it.
  • You get meaningful compiler errors: Great! Fix the errors. Once everything compiles, move your methods back into the original file.

Upvotes: 28

Nikolay Suvandzhiev
Nikolay Suvandzhiev

Reputation: 9055

In my case, I had to change from this:

listItem.sortingOrder -= 1

to this:

listItem.sortingOrder = listItem.sortingOrder - 1

I found it by looking at the problematic function (pointed by the build error message While emitting SIL for 'myBadFunction'), and taking lines of code one by one until it worked, then focusing on the problematic line of code and trying different things. Good luck.

Upvotes: 0

Junaid Mukhtar
Junaid Mukhtar

Reputation: 843

I got the same error and wasted hours until I saw this blunder in my code:

let databaseRef: FIRDatabaseReference? = FIRDatabase.database().reference()

The problem was FIRDatabaseReference? Changed it to

let databaseRef: FIRDatabaseReference = FIRDatabase.database().reference()

and it started working like a charm! Hope this answer helps!

Upvotes: 0

gietal
gietal

Reputation: 139

got this error on Xcode 8 with Swift 3

I had to remove the throws keyword from this:

public extension FileManager {
    public func foo() throws -> URLRelationship {
        return URLRelationship.other
    }
}

to this:

public extension FileManager {
    public func foo() -> URLRelationship {
        return URLRelationship.other
    }
}

Upvotes: 0

nominanza
nominanza

Reputation: 201

In my case, the issue was, that I've had "Math.h" custom file and class in project, importing and using it caused an issue.

The strangest thing, that this file was in project during last 18 days, but everything worked fine. I guess, that it depends on how I'm using it, using it in Objective-C project, doesn't cause any issue.

So it is better to double check classes names.

Upvotes: 0

zevij
zevij

Reputation: 2446

This typically happens when the compiler does not have enough information (despite what you think) to guarantee/determine the state of a statement or a variable within a statement.

For example, imagine you have a dictionary of type [String: String] which you populate with city names as keys and a comma separated list of corresponding zip codes/post codes.

Imagine that somewhere in your code you want to update the list of corresponding codes:

myDict[town] += newZipCode + ","

In this case, the compiler will respond with segmentation fault as town might not be in the dictionary and therefore it cannot guarantee that the above statement will have a valid value.

To resolve this, you should store the current state of myDict[town] in a separate variable allowing you to handle the case of key not in dict and then update the value for the given key:

myDict[town] = guaranteedValue + "," newZipCode + ","

Unfortunately, it is not always straightforward to determine the root cause so I hope this simple example helps.

Upvotes: 2

mkhoshpour
mkhoshpour

Reputation: 855

mine solved by removing this function in archive mode:

debugPrint(someObject)

Upvotes: 0

John Gibb
John Gibb

Reputation: 10773

I ran into this when I had a struct with 32 fields. I removed one field and everything was fine.

Upvotes: 0

Sky
Sky

Reputation: 86

For me trying to assign a get property which I unwrapped lines.last! was causing the issue. This code is invalid syntax, but the compiler would not pick up on it.

    var lines : [Line] = [Line()]

    var line : Line {
        get {
            return lines.last!
        } set {
            lines.last! = newValue
        }
    }

Upvotes: 0

Gerd Castan
Gerd Castan

Reputation: 6849

in my case, I tried to add a function parameter after a variadic parameter.

Reversing parameter sequence and making the variadic parameter the last parameter in the parameter list fixed it.

Upvotes: 1

Alex
Alex

Reputation: 919

As others wrote above, for me this happened when I'm using an extension over a protocol but the signature of methods in the protocol don't match the implementations in an extension.

In my case, I had added a new parameter to the implementation (in the extension) but forgot to also add it to the method's signature in the protocol.

Upvotes: 1

LunaCodeGirl
LunaCodeGirl

Reputation: 5630

I ran across this today as well. In my case I implemented the protocol in a unit test file and forgot to implement one method that the default protocol implementation had left out.

Upvotes: 0

victor.vasilica
victor.vasilica

Reputation: 1511

I got this error while extending one of my protocols and mistyped and optional type argument.

protocol SomeProtocolName: class {
    var someProtocolVariable: String { get set }

    func someProtocolFunction(someProtocolVariable: String)
}

// MARK:
extension SomeProtocolName {
    func someProtocolFunction(someProtocolVariable: String?) {
        self.someProtocolVariable = someProtocolVariable
    }
}

The difference in function arguments String in prototype and String? in extension caused Segmentation Fault 11.

Upvotes: 12

oshevans
oshevans

Reputation: 116

Mine was due to conflict in overriding without optional return...

Base class :

func functionName(bla:String) -> UIView?

Derived class :

func functionName(bla:String) -> UIView

It was failing on the call, not the func declaration.

functionName("Help")

Upvotes: 0

Ajinkya Patil
Ajinkya Patil

Reputation: 5548

I also had same error, but it seemed to me that my Xcode has some issue while making build, after building several times the error was gone,but after I updated my Xcode to 7 the error was gone

Upvotes: 0

mike.tihonchik
mike.tihonchik

Reputation: 6933

Like @Fjohn said, this was an issue related to unwrapping an optional for me (broke in both Xcode 7.0 beta 6 and Xcode 7). In my case, I was not unwrapping optional of the optional (what tipped me off was double ?? in the descriptor. Using if let solved the issue

double optional causing segmentation fault

Upvotes: 1

Related Questions