swiftpostgresqlkitura

SwiftKueryPostgresql connection fails with error "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"


I'm pretty new to Swift, more specifically on Kitura. I'm using SwiftKueryPostgresql to handle communication with database.

I'm writting an ORM library for a specific project. I'm testing my connection like this (as I understood from documentation) :

import Foundation
import SwiftKuery
import SwiftKueryPostgreSQL
import XCTest
@testable import myprojet

final class project_ormTests: XCTestCase {
    var context: Context? = Context(pool: PostgreSQLConnection.createPool(host: "localhost", port: 5432, options: [.databaseName("project"), .userName("myproject"), .password("")], poolOptions: ConnectionPoolOptions(initialCapacity: 3)).getConnection())
    }

    func testConnection() {

        XCTAssertNotNil(context)
    }

    static var allTests = [
        ("testConnection", testConnection),
    ]
}

My class Context :

public class Context {
    private let pool : Connection

    public init?(pool : Connection?) {
        guard pool != nil else { return nil }
        self.pool = pool!
    }
}

When I run testConnection() on xcode, I reach SwiftKuery/ConnectionPool.swift on line 129 with this error

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 

What am I doing wrong ? Is there something I missed ? I really have no idea what to do...

Here's my Package.swift if it can help

import PackageDescription

let package = Package(
    name: "project",
    products: [
        .library(
            name: "projet",
            targets: ["project"]),
    ],
    dependencies: [
        .package(url: "https://github.com/IBM-Swift/Swift-Kuery-PostgreSQL.git", from: "1.2.0")
    ],
    targets:[
        .target(
            name: "project",
            dependencies: ["SwiftKueryPostgreSQL"]),
        .testTarget(
            name: "projectTests",
            dependencies: ["project"]),
    ]
)

Thank you


Solution

  • The Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) error normally happens when force unwrapping and optional which is set to nil. I would suggest instead of doing self.pool = pool!, you can do guard self.pool = pool else { return nil } The guard statement will unwrap the optional value for you and if it can't if will fall to the else.

    A few things I noticed: - In your Package.swift the SwiftKuery dependency is missing. - It seems you have a typo in testable import myprojet

    The line the error is pointing to is the deinit function for the Connection. This function gets called when the Connection is out of scope or not referenced by another object to clear the memory. You may be trying to access the Connection after it has been deleted thus getting an error throned.