Objective-C 中的参数值因函数调用而被破坏

发布于 2024-12-11 04:45:56 字数 800 浏览 0 评论 0原文

所以我似乎遇到了一个零星的问题,到目前为止,没有任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海之角 2024-12-18 04:45:56

您的问题可能是您以错误的方式记录对象。
当您记录 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文