ios 对象可能在被释放后被修改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6317200/
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-08-30 20:17:55  来源:igfitidea点击:

Object was probably modified after being freed

iosmemory-managementautorelease

提问by Stone

I am working on a project on iPhone. I am now initiating a new UIViewController from another UIViewController, and then switch between them. Here is my code.

我正在 iPhone 上开展一个项目。我现在从另一个 UIViewController 启动一个新的 UIViewController,然后在它们之间切换。这是我的代码。

iGreenAppDelegate *delegate = [UIApplication sharedApplication].delegate;
if(checkInViewController) {
    [checkInViewController release];
    checkInViewController = nil;
}
checkInViewController = [[CheckInViewController alloc] initWithCheckpoint:checkpoint];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.8];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:[delegate window] cache:YES];
[[delegate rootTabBarController].view removeFromSuperview];
[[delegate window] addSubview:checkInViewController.view];
[UIView commitAnimations];

The Problem is the second time I initiate the UIViewController, I want to release it to avoid causing memory leak. The Debugger displays

问题是我第二次启动 UIViewController 时,我想释放它以避免导致内存泄漏。调试器显示

iGreen(916,0x3f60348c) malloc: error for object 0x130350: incorrect checksum for freed object - object was probably modified after being freed. set a breakpoint in malloc_error_break to debug

iGreen(916,0x3f60348c) malloc:对象 0x130350 错误:已释放对象的校验和不正确 - 对象可能在被释放后被修改。在 malloc_error_break 中设置断点进行调试

This is strange because similar codes in other parts don't return such error. Moreover, I tried autorelease, but the program will immediately crash and the Debugger says I am modifying finalized layers.

这很奇怪,因为其他部分的类似代码不会返回此类错误。此外,我试过自动释放,但程序会立即崩溃,调试器说我正在修改最终的层。

I've been working on the problem for a whole night, and still confused about it.

我已经整晚都在解决这个问题,但仍然对此感到困惑。

回答by bbum

Set a breakpoint in malloc_error_break to debug.

在 malloc_error_break 中设置断点进行调试。

Do that and post the backtrace.

这样做并发布回溯。

Usually, this means that you corrupted memory, but it may also mean that you have an over-released object. Try Build and Analyze, too.

通常,这意味着您损坏了内存,但也可能意味着您有一个过度释放的对象。也尝试构建和分析。

回答by Niels Castle

Apart from setting a breakpoint in malloc_error_break - press Command-6 in xCode to jump to the breakpoints tab - also enable the malloc aids in your scheme.

除了在 malloc_error_break 中设置断点 - 在 xCode 中按 Command-6 跳转到断点选项卡 - 还可以在您的方案中启用 malloc 辅助。

Go the the schemes selector, choose "Edit scheme" find the "Run" target and go to the "Diagnostics" tab. Below memory management enable scribble, guard edges, guard malloc and zombie objects.

转到方案选择器,选择“编辑方案”,找到“运行”目标并转到“诊断”选项卡。下面的内存管理启用涂鸦、保护边缘、保护 malloc 和僵尸对象。

With a bit of luck xCode will catch you writing outside your allocated memory and corrupting memory.

幸运的是,xCode 会发现您在分配的内存之外写入并损坏内存。

It's like adult supervision for dealing with memory...

这就像处理记忆的成人监督......

回答by metamatt

Understand the error message: it's saying that something continued using (and modifying) the object after you freed it. This code frees it and does not modify it thereafter, but you have to ask what else could possibly continue using it (without knowing it was already freed).

了解错误消息:它表示在您释放对象后某些内容继续使用(和修改)该对象。这段代码释放了它并且此后不会修改它,但是您必须询问还有什么可能继续使用它(不知道它已经被释放了)。

Each time the code in this snippet runs, it releases (frees) any existing checkinViewController, and allocates a new one, and clearly it never touches the old one again. But who else may have a pointer to the old object?

每次运行此代码段中的代码时,它都会释放(释放)任何现有的 checkinViewController,并分配一个新的,显然它再也不会触及旧的。但是还有谁可能拥有指向旧对象的指针?

Possibly other code you wrote, and possibly [delegate window], which gets a reference via "[[delegate window] addSubview:checkInViewController.view];"? Hopefully the latter takes its own reference, meaning release won't immediately free it.

可能是您编写的其他代码,也可能是 [delegate window],它通过“[[delegate window] addSubview:checkInViewController.view];”获得引用?希望后者有自己的参考,这意味着发布不会立即释放它。

But watch out for anywhere you're copying that pointer without adding a reference. If you do this somewhere, and then elsewhere (such as the above snippet) someone calls release on the same pointer, you may now have a pointer to an object that's been freed.

但是要注意在不添加引用的情况下复制该指针的任何地方。如果您在某处执行此操作,然后在其他地方(例如上面的代码段)有人在同一指针上调用 release,则您现在可能拥有一个指向已释放对象的指针。

回答by tsakoyan

There are a couple of things going wrong design wise in your code. First your release the checkInViewController without removing its view from its superview (if any), then you remove the rootTabBarController's view from its superview, without doing anything to the controller itself, and you don't add the checkInViewController to the rootTabBarController or the rootViewController property of the window, so it's in the air (just retained by your current object). What happens when this (current) object gets deallocated but the view of the checkInViewController stays put (retained by) on the window?

在您的代码中,有几件事情在设计上是错误的。首先你释放 checkInViewController 而不从它的超级视图中删除它的视图(如果有的话),然后你从它的超级视图中删除 rootTabBarController 的视图,而不对控制器本身做任何事情,并且你没有将 checkInViewController 添加到 rootTabBarController 或 rootViewController 属性的窗口,所以它在空中(只是由您当前的对象保留)。当这个(当前)对象被释放但 checkInViewController 的视图保持放置(保留)在窗口上时会发生什么?

If you release your checkInViewController but its view is still retained by the window, it is probably going to create some issues...

如果您释放 checkInViewController 但它的视图仍保留在窗口中,则可能会产生一些问题...

About the error, I think there's somewhere a weak reference (not retained) to your object that acts on it after it's freed.

关于错误,我认为在某个地方存在对您的对象的弱引用(未保留),该对象在释放后对其起作用。