MacRuby指向typedef结构体的指针 [英] MacRuby Pointer to typedef struct
问题描述
此代码与Semgentation故障:11失败,我不明白为什么
framework'Cocoa'
框架'CoreFoundation'
框架'安全'
* keychainObject = Pointer.new_with_type('^ {OpaqueSecKeychainRef}')
SecKeychainOpen(/ Users / charbon / Library / Keychains / Josja.keychain ,keychainObject)
SecKeychainLock(keychainObject)
我确定它必须keychainObject类型,因为它工作(它锁定默认keychain)。
SecKeychainLock(nil)
我使用'^ {OpaqueSecKeychainRef}'作为指针的类型,因为这是调试器告诉我,错误类型的指针。
b $ b 作为参考,完整的输出是 如果你在写C,你会写成 而SecKeychainOpen需要一个 this code fails with Semgentation Fault: 11, and I can't understand why I'm sure it has to do with the keychainObject type, because this works (it locks the default keychain). 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
If you were writing C you would have written i.e. while SecKeychainOpen requires a pointer to a
这篇关于MacRuby指向typedef结构体的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
cobalt:〜charbon $ macirb Desktop / test.rb
irb(main):001:0>框架'Cocoa'
=> true
irb(main):002:0>框架'CoreFoundation'
=> true
irb(main):003:0>框架'安全'
=> 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)
分段错误:11
SecKeychainRef keyChainRef;
SecKeychainOpen(/ path / to / ...,& keychainRef);
SecKeychainLock(keyChainRef);
SecKeychainRef
的指针(以便输出参数可以填写),其他apis只需要一个 SecKeychainRef
,因此您需要取消引用指针:
framework'Security'
keychainObject = Pointer.new_with_type ^ {OpaqueSecKeychainRef}')
SecKeychainOpen(/ Users / charbon / Library / Keychains / Josja.keychain,keychainObject)
SecKeychainLock(keychainObject.value)
framework 'Cocoa'
framework 'CoreFoundation'
framework 'Security'
* keychainObject = Pointer.new_with_type('^{OpaqueSecKeychainRef}')
SecKeychainOpen("/Users/charbon/Library/Keychains/Josja.keychain",keychainObject)
SecKeychainLock(keychainObject)
SecKeychainLock(nil)
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
SecKeychainRef keyChainRef;
SecKeychainOpen("/path/to/...", &keychainRef);
SecKeychainLock(keyChainRef);
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)