Skip to content

Example fails with SIGSEGV on MacOS with Nim 2.2.2 and default orc memory manager #53

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

Open
martin-c opened this issue Feb 9, 2025 · 2 comments

Comments

@martin-c
Copy link
Contributor

martin-c commented Feb 9, 2025

The first example

import serial

for port in listSerialPorts():
  echo port

compile with
nim c --stacktrace:on example.nim

Compilation works es expected. But running the produced binary results in

Traceback (most recent call last)
/Users/martin/.nimble/pkgs2/serial-1.2.0-85079d0e3a6e3f1478357c016e03308b8ede3a05/serial/private/utils/list_serialports_mac.nim(96) example
/Users/martin/.nimble/pkgs2/serial-1.2.0-85079d0e3a6e3f1478357c016e03308b8ede3a05/serial/private/utils/list_serialports_mac.nim(84) getDeviceName
/usr/local/Cellar/nim/2.2.2/nim/lib/system/arc.nim(188) nimRawDispose
/usr/local/Cellar/nim/2.2.2/nim/lib/system/alloc.nim(1165) dealloc
/usr/local/Cellar/nim/2.2.2/nim/lib/system/alloc.nim(1063) rawDealloc
/usr/local/Cellar/nim/2.2.2/nim/lib/system/alloc.nim(815) addToSharedFreeListBigChunks
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Compiling with --mm:refc creates binary which works, but no serial ports are listed. (MacOS 15.3)

@euantorano
Copy link
Owner

euantorano commented Feb 9, 2025 via email

@euantorano
Copy link
Owner

As an experiment, I've created the same code in Swift, and it compiles and runs fine there. I'm trying to work out what Nim is doing differently, but I suspect it's something related to ORC.

Example Swift implementation:

import Foundation

func listSerialPorts() {
    var it: io_iterator_t = io_iterator_t()
    
    let kernResult = withUnsafeMutablePointer(to: &it) { itPtr in
        IOServiceGetMatchingServices(
            kIOMainPortDefault,
            IOServiceMatching("IOSeriaolBSDClient"),
            itPtr
        )
    }
    
    if kernResult == KERN_SUCCESS {
        defer {
            IOObjectRelease(it)
        }
        
        while IOIteratorIsValid(it) != 0 {
            let service = IOIteratorNext(it)
            
            if service == 0 {
                break
            }
            
            let key = CFStringCreateWithCString(
                kCFAllocatorDefault,
                "IOCalloutDevice",
                kCFStringEncodingASCII
            )
            
            let cfContainer = IORegistryEntryCreateCFProperty(
                service,
                key,
                kCFAllocatorDefault,
                0
            )
            
            if let cfContainer {
                let name = CFStringGetCStringPtr((cfContainer as! CFString), kCFStringEncodingASCII)
                
                print(String(cString: name!))
            }
        }
    }
}

listSerialPorts()

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

2 participants