Search code examples
macosdelphifiremonkey

Delphi on OSX: how to convert CFDictionaryRef to NSDictionary?


According to Apple documentation CFDictionaryRef and NSDictionary are Toll-Free Bridged Types. However, I get an AV in the following code, which MIGHT be because the bridge does not work with Delphi.

DADiskCopyDescription returns a CFDictionaryRef, which I wrap to a NSDictionary. The pointer is not NIL, but when I access a function in the object, I get an AV:

function osxGetRootVolumeUUID: string;
var lNSDictionary: NSDictionary;
    lDisk: DADiskRef;
    lCFUrl: CFURLRef;
    lSession: DASessionRef;
    lUUID: CFUUIDRef;
begin
  result := '';
  lSession := DASessionCreate(nil);
  lCFUrl := TNSURL.OCClass.fileURLWithPath(StrToNSStr('/'));
  lDisk := DADiskCreateFromVolumePath(nil,lSession,lCFUrl);
  lNSDictionary := TNSDictionary.Wrap(DADiskCopyDescription(lDisk));

  lUUID := lNSDictionary.objectForKey(kDADiskDescriptionVolumeUUIDKey); //<- AV here

  if lUUID<>nil then
    result := CFStringRefToStr( CFUUIDCreateString(nil, lUUID));
  lNSDictionary.release;
  CFRelease(lCFurl);
end;

I also tried to use CFBridgingRelease as suggested in this SO question but it gives error SIGABRT (6):

lNSDictionary := TNSDictionary.Wrap(CFBridgingRelease(DADiskCopyDescription(lDisk))); //gives error: SIGABRT (6)

How to make it work?

PS. Basically I just want to get the UUID of the root volume on OSX. The function above is converted to Delphi from an Objective-C function we have used so far.

Edit

Another thing I have tried is to avoid using NSDictionary, but this gives an error too, which could indicate that the problem is something else:

lCFDictionaryRef := DADiskCopyDescription(lDisk);
lUUID := CFDictionaryGetValue(lCFDictionaryRef, kDADiskDescriptionVolumeUUIDKey); //<- AV here

Solution

  • To convert a CFDictionaryRef to a TNSDictionary you must do it like this :

      var LNSDict: NSDictionary;
          LCFDict: CFDictionaryRef;
    
      LCFDict := DADiskCopyDescription(lDisk);
      try
        LNSDict:= TNSDictionary.Wrap(LCFDict );
      finally
        CFRelease(LCFDict);
      end;
    

    so everything is good here, however i think maybe your av is in the way you access the object. don't forget in IOS to always use getObjectID and not the pointer to the object

    ex: pass to ios api (xxx as ILocalObject).GetObjectID and never pass to the IOS api directly xxx

    so maybe it's must be (kDADiskDescriptionVolumeUUIDKey as ILocalObject).GetObjectID or something similar