Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recurring crash when connection object goes out of scope #1276

Open
ssokol opened this issue Aug 14, 2024 · 0 comments
Open

Recurring crash when connection object goes out of scope #1276

ssokol opened this issue Aug 14, 2024 · 0 comments

Comments

@ssokol
Copy link

ssokol commented Aug 14, 2024

SQLite.swift Version: 0.15.3
X-Code: 15.4
iOS: 17.5.1 on iPad Pro 11-inch (M4)
Included via SPM

I recently started getting crash reports from Crashlytics indicating an issue with the release of a SQLite connection object in a specific module within my app.

The code is extremely simple - it opens a connection to the SQLite file, runs a single query, then exits, allowing the connection object to fall out of scope. Here's the function where it occurs:

    private func findObstaclesWithinBox(bbox:GLMapBBox, minElevation: Int = 0) -> [FBObstacle] {
        
        print("Finding obstacles within box...")
        
        let minX = bbox.origin.x
        let minY = bbox.origin.y
        let maxX = bbox.origin.x + bbox.size.x
        let maxY = bbox.origin.y + bbox.size.y
        
        let mpMin = GLMapPoint(x: minX, y: minY)
        let mpMax = GLMapPoint(x: maxX, y: maxY)
        let gpMin = GLMapView.makeGeoPoint(from: mpMin)
        let gpMax = GLMapView.makeGeoPoint(from: mpMax)
        
        let query = obstacleTable.select(identifier, type, agl, msl, lighting, lat, lng)
            .filter(lat >= (gpMin.lat - 0.1))
            .filter(lng >= (gpMin.lon - 0.1))
            .filter(lat <= (gpMax.lat + 0.1))
            .filter(lng <= (gpMax.lon + 0.1))
            .filter(msl >= minElevation)
            .filter(agl > 150)
        
        var obstacles:[FBObstacle] = []
        if let db = FBDatabaseManager.shared.getDatabaseConnection() {
            do {
                let q = try db.prepare(query)
                for row in q {
                    if let obs = makeObstacleFromRow(row: row) {
                        obstacles.append(obs)
                    }
                }
            } catch {
                print("Something went wrong querying obstacles")
            }
        } // CRASH OCCURS HERE
        return obstacles
    }

This function is executed thousands of times over the life of a typical app instance. It crashes once every one to two, always in the same place. There's nothing unusual about the means by which the database handle is allocated: other elements of the application use the same singleton database manager instance to return a handle without any problem.

I finally managed to catch the crash in the debugger on a test system with Address Sanitizer enabled. Here are a screenshot and a copy of the output from X-Code. Please let me know if you see anything that I'm missing or have any idea what might be causing this.

X-Code Screenshot

image

X-Code Crash Log

2024-08-14 15:14:00.139 -0500: Finding obstacles within box...
=================================================================
==3269==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x00013e2a0aa0 in thread T1116
2024-08-14 15:14:00.180 -0500: FDC: 2024-08-14 21:14:00.0981 | fvTraffic        | Deleting distant target: N400FJ (83.2)
2024-08-14 15:14:00.181 -0500: FDC: 2024-08-14 21:14:00.0991 | fvTraffic        | Deleting distant target: N352I9 (64.5)
2024-08-14 15:14:00.181 -0500: FDC: 2024-08-14 21:14:00.0996 | fvTraffic        | Deleting distant target: N300ML (52.9)
2024-08-14 15:14:00.183 -0500: FDC: 2024-08-14 21:14:00.1008 | fvTraffic        | Deleting distant target: N455UY (54.1)
2024-08-14 15:14:00.184 -0500: FDC: 2024-08-14 21:14:00.1029 | fvTraffic        | Deleting distant target: N483O1 (60.6)
2024-08-14 15:14:00.186 -0500: FDC: 2024-08-14 21:14:00.1039 | fvTraffic        | Deleting distant target: N125E9 (46.2)
    #0 0x106d40558 in __sanitizer_mz_free+0xf8 (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/Frameworks/libclang_rt.asan_ios_dynamic.dylib:arm64e+0x54558)
    #1 0x1ce4a8780 in sqlite3_log+0x11c0c (/usr/lib/libsqlite3.dylib:arm64e+0x2b780)
    #2 0x1ce4a85ac in sqlite3_log+0x11a38 (/usr/lib/libsqlite3.dylib:arm64e+0x2b5ac)
    #3 0x1ce52f6b8 in sqlite3_sourceid+0x21608 (/usr/lib/libsqlite3.dylib:arm64e+0xb26b8)
    #4 0x1ce524870 in sqlite3_sourceid+0x167c0 (/usr/lib/libsqlite3.dylib:arm64e+0xa7870)
    #5 0x1ce4cece8 in sqlite3_bind_int64+0xee0 (/usr/lib/libsqlite3.dylib:arm64e+0x51ce8)
    #6 0x1ce4f1070 in sqlite3_backup_finish+0x2d4 (/usr/lib/libsqlite3.dylib:arm64e+0x74070)
    #7 0x1ce4fb9c4 in sqlite3_txn_state+0x4f4 (/usr/lib/libsqlite3.dylib:arm64e+0x7e9c4)
    #8 0x1042f3c14 in SQLite.Connection.deinit+0x160 (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/FlightBox PFD:arm64+0x101e37c14)
    #9 0x1042f3e80 in SQLite.Connection.__deallocating_deinit+0x15c (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/FlightBox PFD:arm64+0x101e37e80)
    #10 0x1a3297368 in <redacted>+0x34 (/usr/lib/swift/libswiftCore.dylib:arm64e+0x3ab368)
    #11 0x1a3298484 in <redacted>+0x84 (/usr/lib/swift/libswiftCore.dylib:arm64e+0x3ac484)
    #12 0x10266b45c in FlightBox_PFD.FBObstacleFactory.(findObstaclesWithinBox in _B4968670F7213BBAF4E566B92458E66A)(bbox: __C.GLMapBBox, minElevation: Swift.Int) -> Swift.Array<FlightBox_PFD.FBObstacle>+0x9a0c (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/FlightBox PFD:arm64+0x1001af45c)
    #13 0x10265f3f4 in FlightBox_PFD.FBObstacleFactory.displayObstaclesWithinBBox(altitude: Swift.Double) -> ()+0x1a4c (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/FlightBox PFD:arm64+0x1001a33f4)
    #14 0x102647040 in closure #1 @Sendable () -> () in FlightBox_PFD.FBObstacleFactory.locationUpdate(notification: Foundation.Notification) -> ()+0x2a4 (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/FlightBox PFD:arm64+0x10018b040)
    #15 0x102582840 in reabstraction thunk helper from @escaping @callee_guaranteed @Sendable () -> () to @escaping @callee_unowned @convention(block) @Sendable () -> ()+0xa4 (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/FlightBox PFD:arm64+0x1000c6840)
    #16 0x106d3e5bc in __wrap_dispatch_async_block_invoke+0xc0 (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/Frameworks/libclang_rt.asan_ios_dynamic.dylib:arm64e+0x525bc)
    #17 0x1091c0b94 in _dispatch_call_block_and_release+0x1c (/usr/lib/system/introspection/libdispatch.dylib:arm64e+0x4b94)

    #18 0x1091c27b8 in _dispatch_client_callout+0x10 (/usr/lib/system/introspection/libdispatch.dylib:arm64e+0x67b8)
    #19 0x1091ca668 in _dispatch_lane_serial_drain+0x33c (/usr/lib/system/introspection/libdispatch.dylib:arm64e+0xe668)
    #20 0x1091cb404 in _dispatch_lane_invoke+0x194 (/usr/lib/system/introspection/libdispatch.dylib:arm64e+0xf404)
    #21 0x1091d8400 in _dispatch_root_queue_drain_deferred_wlh+0x144 (/usr/lib/system/introspection/libdispatch.dylib:arm64e+0x1c400)
    #22 0x1091d7a34 in _dispatch_workloop_worker_thread+0x1b8 (/usr/lib/system/introspection/libdispatch.dylib:arm64e+0x1ba34)
    #23 0x2013fa930 in _pthread_wqthread+0x11c (/usr/lib/system/libsystem_pthread.dylib:arm64e+0x4930)
    #24 0x2013f70c8 in start_wqthread+0x4 (/usr/lib/system/libsystem_pthread.dylib:arm64e+0x10c8)

0x00013e2a0aa0 is located 240 bytes after 32-byte region [0x00013e2a0990,0x00013e2a09b0)
allocated by thread T0 here:
2024-08-14 15:14:00.187 -0500: FDC: 2024-08-14 21:14:00.1058 | fvTraffic        | Deleting distant target: N064DY (69.0)
2024-08-14 15:14:00.188 -0500: FDC: 2024-08-14 21:14:00.1066 | fvTraffic        | Deleting distant target: N4153V (84.1)
2024-08-14 15:14:00.189 -0500: FDC: 2024-08-14 21:14:00.1074 | fvTraffic        | Deleting distant target: N113V8 (59.6)
2024-08-14 15:14:00.189 -0500: FDC: 2024-08-14 21:14:00.1079 | fvTraffic        | Deleting distant target: N316EA (70.4)
2024-08-14 15:14:00.190 -0500: FDC: 2024-08-14 21:14:00.1089 | fvTraffic        | Deleting distant target: N86698 (30.9)
    #0 0x106d3fba0 in wrap_calloc+0x9c (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/Frameworks/libclang_rt.asan_ios_dynamic.dylib:arm64e+0x53ba0)
    #1 0x1b47f6c80 in <redacted>+0x64 (/usr/lib/system/libsystem_malloc.dylib:arm64e+0x1bc80)
    #2 0x19c51dae4 in <redacted>+0x90 (/usr/lib/libobjc.A.dylib:arm64e+0xdae4)
    #3 0x19c51dd64 in <redacted>+0x310 (/usr/lib/libobjc.A.dylib:arm64e+0xdd64)
    #4 0x19c51cfac in <redacted>+0x190 (/usr/lib/libobjc.A.dylib:arm64e+0xcfac)
    #5 0x19c515144 in <redacted>+0x36c (/usr/lib/libobjc.A.dylib:arm64e+0x5144)
    #6 0x19c5134c0 in <redacted>+0x40 (/usr/lib/libobjc.A.dylib:arm64e+0x34c0)
    #7 0x1af9ea1c8 in <redacted>+0x98 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x411c8)
    #8 0x1af9e9090 in <redacted>+0x68 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x40090)
    #9 0x1af9e7f90 in <redacted>+0x6a0 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x3ef90)
    #10 0x1af9f5cdc in <redacted>+0x94 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x4ccdc)
    #11 0x1af9f7e5c in <redacted>+0x734 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x4ee5c)
    #12 0x1af9f76f8 in <redacted>+0xcc (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x4e6f8)
    #13 0x1af9f6614 in <redacted>+0x1dc (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x4d614)
    #14 0x1af9d669c in <redacted>+0x43c (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x2d69c)
    #15 0x1af9f7334 in <redacted>+0xb4 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x4e334)
    #16 0x1af9d9f00 in <redacted>+0x3e8 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x30f00)
    #17 0x1afa087a8 in <redacted>+0x12c (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0x5f7a8)
    #18 0x1afa5de08 in <redacted>+0x28 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0xb4e08)
    #19 0x1afa5b028 in <redacted>+0x454 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0xb2028)
    #20 0x1afa5dcf0 in <redacted>+0xb0 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0xb4cf0)
    #21 0x1af9b4aac in <redacted>+0xa28 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0xbaac)
    #22 0x1af9b3c5c in <redacted>+0x280 (/System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation:arm64e+0xac5c)
    #23 0x1a6938e14 in <redacted>+0x6c (/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore:arm64e+0xf2e14)
    #24 0x1a6935ab8 in <redacted>+0x1d4 (/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore:arm64e+0xefab8)
    #25 0x1a690c5f0 in <redacted>+0x1bc (/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore:arm64e+0xc65f0)
    #26 0x1a6915ecc in <redacted>+0x1b4 (/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore:arm64e+0xcfecc)
    #27 0x1a6915be0 in <redacted>+0x18 (/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore:arm64e+0xcfbe0)
    #28 0x1a6916290 in <redacted>+0x4c (/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore:arm64e+0xd0290)
    #29 0x1041f173c in FlightBox_PFD.MFDViewController.(heightForView in _2E534678638008F374288BF91470636D)(text: __C.NSAttributedString) -> CoreGraphics.CGFloat+0x6c4 (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/FlightBox PFD:arm64+0x101d3573c)

Thread T1116 created by T1115 here:
    <empty stack>

Thread T1115 created by T9 here:
    <empty stack>

Thread T9 created by T4 here:
    <empty stack>

Thread T4 created by T2 here:
    <empty stack>

Thread T2 created by T0 here:
    <empty stack>

SUMMARY: AddressSanitizer: bad-free (/private/var/containers/Bundle/Application/2A2E4E89-4163-4145-948C-DCB323F9A064/FlightBox PFD.app/Frameworks/libclang_rt.asan_ios_dynamic.dylib:arm64e+0x54558) in __sanitizer_mz_free+0xf8
==3269==ABORTING
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant