简体   繁体   English

打印NSInteger时奇怪的BAD_ACCESS

[英]Strange BAD_ACCESS when printing NSInteger

this is my first time asking something here, so don't be too harsh on me please :-). 这是我第一次在这里问问题,所以请不要对我太苛刻:-)。 I have a strange "bad access" issue. 我有一个奇怪的“访问错误”问题。 In a class of mine I have a NSInteger along with a few other members. 在我的一类中,我有一个NSInteger以及其他一些成员。 I override the - (NSString *)description method to print the values of everything like so (omitted the unrelevant part): 我重写了- (NSString *)description方法来打印所有类似的值(忽略了不相关的部分):

- (NSString *)description {
    return [NSString stringWithFormat:@"Duration:%d", duration];
}

and then I print that using NSLog(@"%@", myObject) which is giving me EXC_BAD_ACCESS without any log messages, regardless of the NSZombieEnabled. 然后我使用NSLog(@"%@", myObject)打印该文件NSLog(@"%@", myObject)这给了我EXC_BAD_ACCESS而没有任何日志消息,而与NSZombieEnabled无关。

  • I have double checked the order of all formatting specifiers and parameters - it's correct. 我仔细检查了所有格式说明符和参数的顺序-是正确的。
  • I tried changing the format specifier to %i and %@ and didn't get any result 我尝试将格式说明符更改为%i%@ ,但未得到任何结果
  • When I init my object I don't initialize duration. 当我初始化对象时,我不会初始化持续时间。 Later, I assign it through a property @property NSInteger duration . 稍后,我通过属性@property NSInteger duration分配它。 So I tried initializing the duration to 0 in my init method but to no avail. 所以我尝试在我的init方法中将持续时间初始化为0,但无济于事。
  • If I box duration to a NSNumber prior to printing, it works. 如果我在打印前将持续时间装箱到NSNumber,则可以使用。
  • When I remove the duration and leave all the other ivars it works again. 当我删除持续时间并保留所有其他ivars时,它将再次起作用。

I'm stumped, what am I doing wrong here? 我很困惑,我在这里做错了什么? Can you help me? 你能帮助我吗?

Thanks a lot! 非常感谢!

EDIT: To summarize, It seems this is caused by differences between 32 and 64 bit platforms, because it's fine when run on an iphone 4 and has issues only when run in the simulator. 编辑:总而言之,这似乎是由32位和64位平台之间的差异引起的,因为在iphone 4上运行时很好,并且仅在模拟器中运行时才有问题。 I tried 3 different approaches - using %d and %i with the NSInteger variable itself, using %qi and using %ld / %lx and I also tried to cast to int and long with the various format specifiers. 我尝试了3种不同的方法-将%d%i与NSInteger变量本身一起使用,使用%qi%ld / %lx并且还尝试使用各种格式说明符将其强制转换为intlong Every time I can run on the device, but get EXC_BAD_ACCESS in the simulator. 每次我都可以在设备上运行,但是要在模拟器中获得EXC_BAD_ACCESS。

这里唯一的猜测:在您的情况下,NSInteger可能是64位,而%i(以及%d)期望使用32位整数,则可以尝试%qi或(似乎更好的)强制转换该值。

When you run the app in the debugger, where exactly is the signal raised? 当您运行在调试器中的应用, 正是在信号提高吗? Xcode will show you the precise line and stack of the problem. Xcode将向您显示问题的确切内容和堆栈。

When you are sure the crash happens in the stringWithFormat: method it's probably a matter of format specifiers. 当您确定崩溃发生在stringWithFormat:方法中时,可能是格式说明符的问题。 Apple's String Programming Guide contains information on how to handle NSInteger in a safe and platform independent way. Apple的《 字符串编程指南》包含有关如何以安全且独立于平台的方式处理NSInteger 信息

也许您需要合成您的财产?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM