Objective-C“发送到释放实例 0x5633b0 的消息”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/601074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:12:30  来源:igfitidea点击:

Objective-C "message sent to deallocated instance 0x5633b0"

iphoneobjective-c

提问by Logan Capaldo

I appear to have some overzealous releasing going on in my obj-C app - getting error message

我的 obj-C 应用程序中似乎有一些过分热心的发布 - 收到错误消息

"-[myobj release]: message sent to deallocated instance 0x5633b0"

“-[myobj release]:消息发送到释放实例 0x5633b0”

. I know the class of the object instance causing the problem, but this class is used all over to create many instances.

. 我知道导致问题的对象实例的类,但是这个类被广泛用于创建许多实例。

My thought is I could put some logging in the init method of the class to log whatever "0x5633b0" corresponds to which should help me track down where the instance is being created.

我的想法是我可以在类的 init 方法中记录一些日志,以记录任何“0x5633b0”对应的内容,这应该可以帮助我跟踪创建实例的位置。

What exactly is the "0x5633b0" and is there any way I can get access to that value in the code to log it?

“0x5633b0”到底是什么,有什么方法可以访问代码中的那个值来记录它?

Thanks.

谢谢。

回答by Ori

What worked best for me when I ran into similar problems recently was the following:

当我最近遇到类似问题时,对我最有效的方法如下:

  1. Under under Project->Edit Active Executable -> Arguments tab -> Environmentvariables section I added and set to YESthe following variables: NSAutoreleaseFreedObjectCheckEnabled, NSZombieEnabledand NSDebugEnabled.

  2. Under the Run menu, I selected Enable Guard Malloc.

  1. Project->Edit Active Executable -> Arguments 选项卡 -> Environmentvariables 部分下,我添加并设置YES了以下变量:NSAutoreleaseFreedObjectCheckEnabled,NSZombieEnabledNSDebugEnabled.

  2. 在 Run 菜单下,我选择了Enable Guard Malloc

With these settings the debugger provided more hints on what's wrong with my code.

通过这些设置,调试器提供了更多关于我的代码有什么问题的提示。

(I found these tips here)

(我在这里找到了这些提示)

Good luck, Ori

祝你好运,奥利

回答by Logan Capaldo

0x5633b0is likely the address of object in question (the value of self). You can use NSLogor printfwith %pto print it.

0x5633b0可能是相关对象的地址( 的值self)。您可以使用NSLogprintfwith%p来打印它。

回答by leviathan

0x5633b0is likely the address of the deallocated object (the value of myobj). You can use NSLogor printfwith %pto print it.

0x5633b0可能是释放对象的地址( 的值myobj)。您可以使用NSLogprintfwith%p来打印它。

You can also use the instruments profiler to find the deallocated object.

您还可以使用仪器分析器来查找已释放的对象。

1. Start the profiler:

1. 启动分析器:

enter image description here

在此处输入图片说明

2. Select the "Zombies" and start the profiler.

2. 选择“Zombies”并启动分析器。

enter image description here

在此处输入图片说明

3. Click through the simulator until you hit your "deallocated error case"

3. 单击模拟器,直到遇到“解除分配的错误情况”

enter image description here

在此处输入图片说明

回答by bbrown

In the debugger, type info symbol 0x5633b0and you'll get some indication as to what object it is. One other thing that might be helpful is backtracewhich will give you a stack trace. All in all, this blog entryhas some greattips.

在调试器中,键入info symbol 0x5633b0,您将得到一些关于它是什么对象的指示。另一件可能有用的事情是backtrace它会给你一个堆栈跟踪。总而言之,这个博客条目有一些很好的提示。

回答by nst

Consider using the NSZombieEnabled flag.

考虑使用 NSZombieEnabled 标志

You will then know what is this deallocated object you're sending a message.

然后,您将知道您正在发送消息的这个释放的对象是什么。

回答by LolaRun

you can also add these to environment variables:
MallocStackLoggingNoCompact 1

您还可以将这些添加到环境变量中:
MallocStackLoggingNoCompact 1

and write in the gdb console:
info malloc-history <paste-address-here>

并在 gdb 控制台中写入:
info malloc-history <paste-address-here>

Reference: here

参考:这里

回答by Adam Rosenfield

You're not managing your memory properly -- you're calling release/autoreleaseon some object more times than you're calling retain. Make sure you're following all of the rules laid out in the Memory Management Programming Guide for Cocoa.

您没有正确管理您的内存——您在某个对象上调用release/ 的autorelease次数比调用 的次数多retain。确保您遵循Cocoa 内存管理编程指南中列出的所有规则。

0x5633b0 is just the address of the memory location at which the object is stored. One thing you can try to do is to add some code to the initmethod:

0x5633b0 只是存储对象的内存位置的地址。您可以尝试做的一件事是向该init方法添加一些代码:

- (void) init
{
    if(self == (MyClass*)0x5633b0)
        NSLog(@"Allocated object at address 0x5633b0");  // put a breakpoint on this line
    // do rest of init...
}

If you have any other initmethods (e.g. initWithCoder:, which is called for objects instantiated from a XIB), make sure to put this snippet in those methods as well. Put a breakpoint on the NSLogline, and then see when it gets hit. Note that it may get hit several times, if an object is allocated at that address, deallocated, and then another object happens to be reallocated at the same address. The last hit before the crash is the one you want.

如果您有任何其他init方法(例如initWithCoder:,为从 XIB 实例化的对象调用的 ),请确保将此代码段也放入这些方法中。NSLog在行上放置一个断点,然后查看它何时被命中。请注意,如果一个对象在该地址分配,释放,然后另一个对象恰好在同一地址重新分配,则它可能会被多次命中。崩溃前的最后一击是你想要的。