简体   繁体   English

仅在iPhone 4S上对代表的访问权限错误

[英]Bad Access on delegate only on iPhone 4S

My application crashes after random times when delegate is called (inside IBAction method) but only on iPhone 4S in Simulator. 调用委托(在IBAction方法内部)后的随机时间后,我的应用程序崩溃了,但仅在Simulator中的iPhone 4S上崩溃了。 Every other newer iPhone works great. 其他每款更新的iPhone都很棒。

@propert (assign, nonatomic) id <MyDelegate> delegate;

Of course I am using ARC. 当然,我正在使用ARC。 What can be the reason? 可能是什么原因?

SOLVED: Argument passing in delegate method in some conditions was unintentionally released from memory. 已解决:在某些情况下,传递给委托方法的参数无意中从内存中释放。 Changing it to strong type helped. 将其更改为强类型会有所帮助。

The reason is that you are using assign . 原因是您正在使用assign This basically means "crash me". 这基本上意味着 “使我崩溃”。 If your delegate goes out of existence, you are left pointing to garbage, and any attempt to access that garbage will result in a crash. 如果您的delegate不存在,您将指向垃圾,任何访问该垃圾的尝试都将导致崩溃。 In effect, you have thrown away the advantage that ARC gives you. 实际上,您已经放弃了ARC为您提供的优势。

To restore that advantage, use weak instead. 要恢复该优势,请改用weak Now if your delegate goes out of existence, you will have nil and can proceed in good order. 现在,如果您的delegate不复存在,您将只有nil而且可以井井有条。

Having gotten rid of the crash, you can now check for nil and try to figure out why your delegate has gone out of existence when you evidently don't expect it to. 摆脱崩溃后,您现在可以检查nil并尝试弄清楚为什么您的delegate显然不希望出现时为什么不存在。 Keep in mind that the problem you are seeing is what we call "diagnostic"; 请记住,您遇到的问题就是我们所说的“诊断”; the delegate may have gone out of existence long ago, or it may never have been assigned in the first place, but figuring that out is a completely separate matter. 代表可能很久以前就已经不复存在,或者可能从来没有被指派过,但是弄清楚那是一个完全独立的问题。

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

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