Segmentation fault: 11 in Xcode 10 beta

Hello,


I'm trying to build my app using Xcode 10 beta - but I get an error wich I don't get on the previous versions. Any idea of what it could be?


While silgen emitConstructor SIL function "@$S14PROJECT25SBMonitoredVehicleJourneyV12directionRef04lineG017publishedLineName9monitored0L4Call15monitoringError24originAimedDepartureTime011destinationq7ArrivalS05delay08courseOfeG00pG00pK00tG00tK005routeG015vehicleLocation0Z4Mode0zG0AcA011SBDirectionG0OSg_SSSgAYSbAA0cM0VAA012SBMonitoringO0OSgA9yA17SBVehicleLocationVSgAA13SBVehicleModeOAYtcfC".
for 'init(directionRef:lineRef:publishedLineName:monitored:monitoredCall:monitoringError:originAimedDepartureTime:destinationAimedArrivalTime:delay:courseOfJourneyRef:originRef:originName:destinationRef:destinationName:routeRef:vehicleLocation:vehicleMode:vehicleRef:)' at /Users/name/Developer/PROJECT/PROJECT/Models/SBMonitoredVehicleJourney.swift:11:8
error: Segmentation fault: 11


struct SBMonitoredVehicleJourney: Equatable, Codable {   
    let directionRef: SBDirectionRef?
    /// Identifies the Line.
    let lineRef: String?
    /// Name or Number by which the line is known to the public.
    let publishedLineName: String?
    /// Whether there is real-time information available for journey; if not present, not known.
    let monitored: Bool
    let monitoredCall: SBMonitoredCall
    let monitoringError: SBMonitoringError?
    let originAimedDepartureTime, destinationAimedArrivalTime, delay, courseOfJourneyRef: String?
    let originRef, originName, destinationRef, destinationName: String?
    /// Identifier of Route that Journey follows.
    let routeRef: String?
    let vehicleLocation: SBVehicleLocation?
    let vehicleMode: SBVehicleMode
    /// Reference to a vehicle
    let vehicleRef: String?
    
    lazy var departureStatus: SBDepartureStatus = {
        if delay != nil {
            let startDate = Date()
            let components = NSDateComponents.duration(from: delay!)
            let endDate = NSCalendar.current.date(byAdding: components as DateComponents, to: startDate)
            
            if let timeInterval = endDate?.timeIntervalSince(startDate) {
                
                switch timeInterval {
                case ...(-60): return .early
                case 0...120: return .ontime
                case 120...: return .delayed
                default: return .noreport
                }
            }
        }
        
        return .noreport
    }()
    
    enum CodingKeys: String, CodingKey {
        case courseOfJourneyRef = "CourseOfJourneyRef"
        case delay = "Delay"
        case destinationAimedArrivalTime = "DestinationAimedArrivalTime"
        case destinationName = "DestinationName"
        case destinationRef = "DestinationRef"
        case directionRef = "DirectionRef"
        case lineRef = "LineRef"
        case monitored = "Monitored"
        case monitoredCall = "MonitoredCall"
        case monitoringError = "MonitoringError"
        case originAimedDepartureTime = "OriginAimedDepartureTime"
        case originName = "OriginName"
        case originRef = "OriginRef"
        case publishedLineName = "PublishedLineName"
        case routeRef = "RouteRef"
        case vehicleLocation = "VehicleLocation"
        case vehicleMode = "VehicleMode"
        case vehicleRef = "VehicleRef"
    }
}
Answered by espen in 315735022

Found the problem - it was in another class:


This was causing the isse:

Cache.imageOfCityBikeAnnotationIcon = UIGraphicsGetImageFromCurrentImageContext()!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .tile)

This fixed it:

Cache.imageOfCityBikeAnnotationIcon = UIGraphicsGetImageFromCurrentImageContext()!.resizableImage(withCapInsets: UIEdgeInsets(), resizingMode: .tile)

Did you do a Clean Build Folder to try to solve the problem ?


Is SBDirectionRef a class of yours ? If so, what is it ?


Just a side note: your switch seem to overlap or miss cases :


switch timeInterval {

case ...(-60): return .early

case 0...120: return .ontime

case 120...: return .delayed

default: return .noreport


Between -60 and 0: noReport ?

At 120, is it onTime or delayed ?

Yeah, several times. Builds fine in 9.4 (9F1027a).


SBDirectionRef is an enum:

enum SBDirectionRef: String, Equatable, Codable {
    case back = "back"
    case go = "go"
    case null = "null"
    case emtpy = "" // Workaround for stop
}


Thanks for point out that! :-)

Problem of seg faut 11 often comes from optional.


Could there be a conflict between


let directionRef: SBDirectionRef?


and


case directionRef = "DirectionRef"


Should try to change the duplicated names, just to check ?


This may also give you some hints:

h ttps://stackoverflow.com/questions/40968517/command-failed-due-to-signal-segmentation-fault-11-while-emitting-ir-sil-funct

Changing the names will not work as it needs to be the same to conform to the protocols (as far as I know, I'm new to development 🙂).


Thanks for the link, I will check it out.

Somewhere, you initialize the struct SBMonitoredVehicleJourney


CXan you show the part of code (complete func if possible) where you do it ?

That's done through SBMonitoredStopVisit.


struct SBMonitoredStopVisit: Equatable, Codable {
    let monitoredVehicleJourney: SBMonitoredVehicleJourney
    let monitoringRef: String
    /// Time at which data was recorded.
    let recordedAtTime: String
    
    enum CodingKeys: String, CodingKey {
        case monitoredVehicleJourney = "MonitoredVehicleJourney"
        case monitoringRef = "MonitoringRef"
        case recordedAtTime = "RecordedAtTime"
    }
}


With:

init?(data: Data) {
        guard let me = try? JSONDecoder().decode(SBMonitoredStopVisit.self, from: data) else { return nil }
        self = me
    }

Filed a bug with case number 40905743

What is surprising is that you say it worked in 9.4 (Swift 4.1).

Note: did you try to select compiler as 4.1, to check if it still works ?


A last try (I remember I read something about this and found this link

h ttps://teamtreehouse.com/community/can-swift-structs-have-optional-stored-properties

)


When you declare in struct

let directionRef: SBDirectionRef?,


force the nil init (should be implicit but…)

let directionRef: SBDirectionRef? = nil.


Additional question: why do you need them as optional ? They are constant anyway.

Yeah! The project setting is set to Swift 4, but does still not work. Will try to convert the project to 4.2 to see if that helps.


They are optional because the JSON I'm importing does not allways include these objects.

OK, but did you try to set to nil explicitely (even if that sounds unneeded).

I haven't tried, but that's really not an option, as they are not always nil… But now it suddently started working without any changes, so I guess the compiler is a bit buggy.

They would not stay nil.


If you declare a const with let in a struct, it is instanciated in its init, then do not change after.


So,


l

et directionRef: SBDirectionRef? = nil


will not freeze to nil, as the value would be initialized at int.

But I once read that this prevents some compiler problems… at no cost.


If the problem has disappeared, so good.

Ah, ok - thanks! It stopped working again for some reason and setting them to nil did not work. 😟

Segmentation fault are really a nightmare, even worse when random.


I looked at my developer's notes for cases where I encountered such seg Fault 11.


Does not seem to match what you experience, but hope that may be of some help.


case 1: I references a class's optional property when the instance did not exist anymore :

instance!.property


case 2 : index in a range is left optional, unwrapped


case 3: forgot to unwrap the element of an array subscript

struct My2DArray {
}
extension My2DArray {
    subscript (j: Int, h: Int) -> Bool? {
}


var anArray     : My2DArray?
if anArray![j, h] {  }

where I should have written

if anArray![j, h]! {  }

No compiler error just seg fault


In all cases, that involved optional.


I see two ways to try to isolate the problem:

- if possible, replace the optionals in struct by non optional ; that will require to adapt when you return JSON value ; and then reestablish optional (one by one) until you get seg fault 11

- change your init? so that it returns an empty object, not an optional when fails ?


Good luck

Thanks for very helpfull input, Claude31. Will certainly look into that.


Got this one now, so not sure if I should spend to much time on this. Rather wait in the next beta 🙂


<unknown>:0: error: fatal error encountered while reading from module ‘PROJECT’; please file a bug report with your project and the crash log

*** DESERIALIZATION FAILURE (please include this section in any bug report) ***
result is ambiguous
Segmentation fault: 11 in Xcode 10 beta
 
 
Q