MacRuby指向typedef结构体的指针 [英] MacRuby Pointer to typedef struct

查看:230
本文介绍了MacRuby指向typedef结构体的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码与Sem​​gentation故障: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

作为参考,完整的输出是

  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


解决方案

如果你在写C,你会写成

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

而SecKeychainOpen需要一个 SecKeychainRef 的指针(以便输出参数可以填写),其他apis只需要一个 SecKeychainRef ,因此您需要取消引用指针:

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


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

解决方案

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)

这篇关于MacRuby指向typedef结构体的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆