Objective-C 中的参数值因函数调用而被破坏
所以我似乎遇到了一个零星的问题,到目前为止,没有任何 Google-fu 能够解决这个问题。在看似随机的点上,函数就会崩溃,经过一番搜寻后,参数似乎会开始被破坏。
例如,
Object * testObject = [[Object alloc] init];
NSLog(@"ID: %d", testObject);
[testFunction:testObject];
...
- (void) testFunction:(id)testObject
{
NSLog(@"ID: %d", testObject);
当发生这种情况时,这种情况下的 Log 语句将无法匹配,当我使用我传递的引用时,会出现 EXC_BAD_ACCESS 警告或其他各种问题。
有时我可以通过在函数上添加“虚拟值”来解决问题,如下
- (void) testFunction:(id)testObject:(int)dummy
{
所示:然后像这样调用它:
[testFunction:testObject:1111];
我的函数声明在 .h/.m 文件中全部匹配,我唯一能猜测的是可能在项目的其他地方,.h 文件中可能缺少相应的函数声明。然而,所讨论的功能总是正确完成的。 (我有双重、三次检查等)。我知道这不是保留/释放问题,虽然我对 Objective-C 比较陌生,但我对此很满意,并且我也通过 Instruments 运行它寻找泄漏,但似乎没有泄漏。有什么想法可能会导致这种情况,以及为什么在更改项目中其他地方看似完全不相关的代码后会出现问题?
So I seem to have a sporadic problem, that no amount of Google-fu has thus far been able to solve. At seemingly random points functions will just break, and after some hunting it appears arguments will begin to get corrupted.
For example
Object * testObject = [[Object alloc] init];
NSLog(@"ID: %d", testObject);
[testFunction:testObject];
...
- (void) testFunction:(id)testObject
{
NSLog(@"ID: %d", testObject);
When this happens the Log statements in this case would fail to match up, giving me EXC_BAD_ACCESS warnings or other various issues when I go to use the reference I passed.
Sometimes I can fix the issue by tacking on a 'Dummy value' to the function like so:
- (void) testFunction:(id)testObject:(int)dummy
{
and then calling it like so:
[testFunction:testObject:1111];
My function declarations all match in the .h/.m files, the only thing I could guess is that potentially elsewhere in the project there might be missing corresponding function declarations in .h files. However the functions in question are always done correctly. (I have double, triple checked etc). I know it's not a retain/release issue, while I am relatively new to Objective-C I have that down pat and I have also ran it through Instruments looking for leaks and there appear to be none. Any thoughts as to what might cause this, and why issues pop up after changing seemingly completely unrelated code elsewhere in the project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题可能是您以错误的方式记录对象。
当您记录
Object
时,您不能像以前那样对其进行格式化。当您记录任何代码时,您需要将其精确格式化,您将收到严重的访问错误。
您确实应该阅读有关正确格式的苹果文档。
您可以在这里执行此操作:
http:// /developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
Your problem is probably that you are logging your objects in the wrong way.
When you log an
Object
you can't just format it like you did.When you log any code you need to format it precisely right you will get bad access errors.
You should really read up on the apple documentation about the right formats.
And you can do this here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html