IORegistryEntrySearchCFProperty crashes

I had an old code written in Swift2, running on OSX 10.11, which calls IORegistryEntrySearchCFProperty

 

        if let session = DASessionCreate(nil) {
            if let disk : DADisk = DADiskCreateFromVolumePath(nil, session, url as CFURL) {
                let ioService : io_service_t = DADiskCopyIOMedia(disk)
                let key = "USB Serial Number"
          
                let options : IOOptionBits = IOOptionBits(kIORegistryIterateParents) |
                    IOOptionBits(kIORegistryIterateRecursively)
          
                let sSerial : CFTypeRef = IORegistryEntrySearchCFProperty(ioService, kIOServicePlane, key as CFString, nil, options)


 

To transition to Swift 4 I need first to transition to Swift 3…

 

And now, IORegistryEntrySearchCFProperty crashes, with the following crash report:

fatal error: unexpectedly found nil while unwrapping an Optional value

2017-10-07 11:33:33.058648+0200 DongleManager[38561:1657024] fatal error: unexpectedly found nil while unwrapping an Optional value

Current stack trace:

0    libswiftCore.dylib                 0x0000000100327130 swift_reportError + 129

1    libswiftCore.dylib                 0x0000000100343b50 _swift_stdlib_reportFatalError + 60

2    libswiftCore.dylib                 0x0000000100134250 specialized specialized StaticString.withUTF8Buffer ((UnsafeBufferPointer) -> A) -> A + 342

3    libswiftCore.dylib                 0x00000001002aee90 partial apply for (_fatalErrorMessage(StaticString, StaticString, file : StaticString, line : UInt, flags : UInt32) -> Never).(closure #2) + 109

4    libswiftCore.dylib                 0x0000000100134250 specialized specialized StaticString.withUTF8Buffer ((UnsafeBufferPointer) -> A) -> A + 342

5    libswiftCore.dylib                 0x00000001002619a0 specialized _fatalErrorMessage(StaticString, StaticString, file : StaticString, line : UInt, flags : UInt32) -> Never + 96

6    MyApp                                   0x0000000100005030 AppDelegate.getSerialAndSize(URL) -> (String, Int) + 732

 

Previously, I did not need to define an allocator. Seems it’s needed now ?

 

Do I need to call with allocator, instead of nil

                let sSerial : CFTypeRef = IORegistryEntrySearchCFProperty(ioService, kIOServicePlane, key as CFString, allocator, options)

 

If so, how to create and use such an allocator : CFAllocator in Swift ?

Powered by WPeMatico

About

You may also like...

Comments are closed.