Console segmentation fault 11

As I don’t seem to be able to understand how to mitigate the issue I have with the declare I use to get a font name in my desktop app, I decided to move the code to a console helper program, so if it crashes, that won’t crash the main app.

I copied the classes and placed the code in the Run event, but I get a Segmentation Fault 11 in CFArrayGetValueAtIndex.

I tried a System.Debuglog(str(aCFArray)) as well as a Print (str(aCFArray)) and it triggers the segmentation fault immediately. I use the CFArray from MacOSLib, and have to admit it is a bit over my league.

It may very well be where it messes up in the Desktop program as well.

Any suggestion ?

Thank you in advance.

[code] if args.Ubound = -1 then quit

dim URL as string = args(0)

dim f as folderitem = GetFolderItem(URL, FolderItem.PathTypeShell)
dim result as string

if f <> nil then
// - We have a True Type Font file, first step to make a CFURL.
// CFURLRef CFURLCreateFromFSRef (
// CFAllocatorRef allocator,
// const struct FSRef *fsRef
// );

declare function CFURLCreateFromFSRef lib "Cocoa" ( allocator as Ptr, fsRef as Ptr ) as Ptr
Dim CFURLRef as Ptr = CFUrlCreateFromFSRef( nil, f.MacFSRef )



if CFUrlRef = nil then
  Msgbox "Failed to get the CFURLRef of folder """+ f.nativePath + """"
else
  'SWIFT
  'func CTFontManagerCreateFontDescriptorsFromURL(_ fileURL: CFURL!) -> CFArray!
  
  declare function CTFontManagerCreateFontDescriptorsFromURL lib "Cocoa" ( fontURL as Ptr) as CFTypeRef
  Dim aCFArray As CFTyperef = CTFontManagerCreateFontDescriptorsFromURL( CFURLRef )
  
  
  'func CTFontDescriptorCopyAttribute(_ descriptor: CTFontDescriptor!,
  '_ attribute: CFString!) -> AnyObject!
  
  declare function CTFontDescriptorCopyAttribute lib "Cocoa" (CTFontDescriptor as Ptr, Attribut as CFStringRef) as CFStringRef
  Dim attribute as CFStringRef = "NSFontVisibleNameAttribute"
  
  declare function CFArrayGetCount lib "CoreFoundation.framework" (theArray as CFTypeRef) as Integer
  declare function CFArrayGetValueAtIndex lib "CoreFoundation.framework" (theArray as CFTypeRef, idx as Integer) as Ptr
  
  Dim ub As Integer = 1 'CFArrayGetCount( aCFArray ) - 1
  
  if ub > -1 then
    Dim aCTFontDescriptorRef As Ptr = CFArrayGetValueAtIndex( aCFArray, 0 ) // <------ Segmentation fault occurs

    // then use CTFontDescriptorCopyAttribute() to get the name
    'System.DebugLog CTFontDescriptorCopyAttribute(aCTFontDescriptorRef, attribute)
    if aCTFontDescriptorRef <> nil then
      result = CTFontDescriptorCopyAttribute(aCTFontDescriptorRef, attribute)
    end if
    
  end if
  // - As we had a valid CFURLRef from a 'Create' or 'Copy' API, we must release it.
  declare sub CFRelease lib "Cocoa" ( ref as ptr )
  CFRelease CFUrlRef
end if

end if

if Result <> “” then
Print Result
else
Print f.name
end if[/code]

Is it just a typo you declare ub as 1 instead of CFArrayGetCount( aCFArray ) - 1?
This way you could of course try to access array elements that don’t exist.

Also, it would probably be good to check if aCFArray isn’t NIL after you declared it.
Basically, a CFArray is much like a Xojo array, expect you can append any class as an object, in other words it’s always an array of Variants. And that the normal CFArray (or NSArray) is an immutable class.

Thank you Ulrich. Jim McKay just posted a new method in the other thread
https://forum.xojo.com/30089-thread-6-crashed-declare-culprit

which seems robust enough to work in the desktop app. So I may not need the console helper after all.