Search code examples
swiftlistimageexc-bad-access

Swift UI - Thread 1: EXC_BAD_ACCESS (code=2


I added system drive icons (NSImage) to my list of drives

Now the app crashes in AppDelegate.swift on line 13 class AppDelegate: NSObject, NSApplicationDelegate { with Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeef3fff48)

I know it's the images I am adding because when I replace that with text it's ok

Here is the class where I add the images.

import SwiftUI

let workspace = NSWorkspace.init()

struct DriveList: View {
   let drives = SDCardTools.getDrives()!
   var body: some View {
       return List(drives) { drive in
           DriveRow(drive : drive)
       }
   }
}

struct DriveRow: View {
   var drive : Drive
   
   var body: some View {
       HStack {
           DriveIcon(path : drive.path)
               padding(10)
           Text(drive.name)
       }
   }
}

struct DriveIcon : View {
   
   var path: String
   var body: some View {
       Image(nsImage: workspace.icon(forFile: path ))
           .resizable()
           .frame(width: 50, height: 50)
   }
}

struct DriveList_Previews: PreviewProvider {
   static var previews: some View {
       DriveList()
   }
}

Thread 1 Queue : com.apple.main-thread (serial) #0 0x00007fff44382883 in specialized static EnvironmentReadingView._makeView(view:inputs:) () #1 0x00007fff44384668 in protocol witness for static View._makeView(view:inputs:) in conformance Image () #2 0x00007fff44384610 in protocol witness for static View._makeView(view:inputs:) in conformance Image () #3 0x00007fff441c20e9 in TypedUnaryViewGenerator.makeView(in:inputs:id:indirectMap:) () #4 0x00007fff441c21f9 in protocol witness for UnaryViewGenerator.makeView(in:inputs:id:indirectMap:) in conformance TypedUnaryViewGenerator () #5 0x00007fff441bc9b0 in closure #1 in UnaryElements.makeElements(from:in:inputs:indirectMap:body:) () #6 0x00007fff441c4801 in partial apply for closure #1 in UnaryElements.makeElements(from:in:inputs:indirectMap:body:) () #7 0x00007fff441bf61b in closure #1 in closure #1 in closure #1 in closure #1 in ModifiedElements.makeElements(from:in:inputs:indirectMap:body:) () #8 0x00007fff441ce4c5 in partial apply for closure #1 in closure #1 in closure #1 in closure #1 in ModifiedElements.makeElements(from:in:inputs:indirectMap:body:) () #9 0x00007fff445adb15 in specialized static UnaryLayout<>.makeViewImpl(modifier:inputs:body:) () #10 0x00007fff4430e339 in specialized static UnaryLayout._makeView(modifier:inputs:body:) () #11 0x00007fff4430f101 in protocol witness for static ViewModifier._makeView(modifier:inputs:body:) in conformance _AspectRatioLayout () #12 0x00007fff4430eec8 in protocol witness for static ViewModifier._makeView(modifier:inputs:body:) in conformance _FrameLayout () #13 0x00007fff441bf2a5 in closure #1 in closure #1 in closure #1 in ModifiedElements.makeElements(from:in:inputs:indirectMap:body:) () #14 0x00007fff441ce43d in partial apply for closure #1 in closure #1 in closure #1 in ModifiedElements.makeElements(from:in:inputs:indirectMap:body:) () #15 0x00007fff441813ef in closure #2 in static _Layout<>.makeStaticView(root:inputs:list:) ()

Any help you can provide would be gratefully appreciated :)


Solution

  • The bug is very confusing, it is due to inalienable nature of padding modifier... and compiler pass it w/o use it as actually modifier, which result in crash.

    Here is fix

    struct DriveRow: View {
        var drive : Drive
        
        var body: some View {
            HStack {
                DriveIcon(path : drive.path)
                    .padding(10)            // << in this line you missed '.' dot
                Text(drive.name)
            }
        }
    }