0

this code fails with Semgentation Fault: 11, and I can't understand why

framework 'Cocoa'
framework 'CoreFoundation'
framework 'Security'
* keychainObject = Pointer.new_with_type('^{OpaqueSecKeychainRef}')
SecKeychainOpen("/Users/charbon/Library/Keychains/Josja.keychain",keychainObject)
SecKeychainLock(keychainObject)

I'm sure it has to do with the keychainObject type, because this works (it locks the default keychain).

SecKeychainLock(nil) 

I'm using the '^{OpaqueSecKeychainRef}' as the type of pointer because that's what the debugger told me it expected when I used a wrong type of pointer.

I hope solving this would help grasping the macruby / cocoa magic.

For reference, the complete output is

cobalt:~ charbon$ macirb Desktop/test.rb 
irb(main):001:0> framework 'Cocoa'
=> true
irb(main):002:0> framework 'CoreFoundation'
=> true
irb(main):003:0> framework 'Security'
=> true
irb(main):004:0> * keychainObject = Pointer.new_with_type('^{OpaqueSecKeychainRef}')
=> [#<Pointer:0x4007ac200>]
irb(main):005:0> SecKeychainOpen("/Users/charbon/Library/Keychains/Josja.keychain",keychainObject)
=> 0
irb(main):006:0> SecKeychainLock(keychainObject)
Segmentation fault: 11
MichaelC
  • 357
  • 2
  • 12

1 Answers1

1

If you were writing C you would have written

SecKeychainRef keyChainRef;
SecKeychainOpen("/path/to/...", &keychainRef);
SecKeychainLock(keyChainRef);

i.e. while SecKeychainOpen requires a pointer to a SecKeychainRef (so that the output parameter can be filled in), other apis just require a SecKeychainRef, so you need to dereference the pointer:

framework 'Security'
keychainObject = Pointer.new_with_type('^{OpaqueSecKeychainRef}')
SecKeychainOpen("/Users/charbon/Library/Keychains/Josja.keychain",keychainObject)
SecKeychainLock(keychainObject.value)
Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • Thanks Fred for shiming in ! Should'nt the third line be with a * before keychainObject ? SecKeychainOpen("/Users/charbon/Library/Keychains/Josja.keychain",*keychainObject) – MichaelC Jul 27 '13 at 19:07
  • Nope. * in ruby is something completely different – Frederick Cheung Jul 27 '13 at 19:29
  • All right, got it, thanks. Posted the "reverse" question here : http://stackoverflow.com/questions/17902293/macruby-pointer-referencing-dereferencing-when-using-cocoa-frameworks – MichaelC Jul 27 '13 at 20:47