MacRuby Pointer to typedef struct

≡放荡痞女 提交于 2019-12-13 03:41:52

问题


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

回答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)


来源:https://stackoverflow.com/questions/17899102/macruby-pointer-to-typedef-struct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!