对象在释放后可能被修改
我正在做一个关于 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];
问题是我第二次启动 UIViewController 时,我想释放它以避免导致内存泄漏。调试器显示
iGreen(916,0x3f60348c) malloc:对象 0x130350 错误:已释放对象的校验和不正确 - 对象可能在释放后被修改。 在malloc_error_break中设置断点进行调试
这很奇怪,因为其他部分中的类似代码不会返回此类错误。此外,我尝试了自动释放,但程序会立即崩溃,调试器说我正在修改最终层。
我已经为这个问题研究了一整晚,但仍然很困惑。
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.
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
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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 malloc_error_break 中设置断点进行调试。
执行此操作并发布回溯。
通常,这意味着您损坏了内存,但也可能意味着您有一个过度释放的对象。也尝试构建和分析。
Set a breakpoint in malloc_error_break to debug.
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.
除了在 malloc_error_break 中设置断点(在 xCode 中按 Command-6 跳转到断点选项卡)之外,还要在方案中启用 malloc 辅助功能。
转到方案选择器,选择“编辑方案”,找到“运行”目标,然后转到“诊断”选项卡。下面的内存管理启用了涂鸦、保护边缘、保护 malloc 和僵尸对象。
如果运气好的话,xCode 会发现您在分配的内存之外进行写入并损坏内存。
这就像成人对记忆的监督......
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.
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.
With a bit of luck xCode will catch you writing outside your allocated memory and corrupting memory.
It's like adult supervision for dealing with memory...
了解错误消息:它表示在您释放该对象后,某些内容继续使用(并修改)该对象。此代码释放它并且此后不会修改它,但您必须询问还有什么可能继续使用它(不知道它已经被释放)。
每次运行此代码片段中的代码时,它都会释放(释放)任何现有的 checkinViewController,并分配一个新的,并且显然它不会再触及旧的。但是还有谁可能拥有指向旧对象的指针呢?
可能是您编写的其他代码,也可能是[委托窗口],它通过“[[委托窗口] addSubview:checkInViewController.view];”获取引用?希望后者有自己的参考,这意味着发布不会立即释放它。
但请注意在不添加引用的情况下复制该指针的任何地方。如果您在某处执行此操作,然后在其他地方(例如上面的代码片段)有人在同一指针上调用release,那么您现在可能有一个指向已释放的对象的指针。
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?
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.
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.
您的代码中存在一些设计上的错误。首先释放 checkInViewController 而不从其超级视图(如果有)中删除其视图,然后从其超级视图中删除 rootTabBarController 的视图,而不对控制器本身执行任何操作,并且不将 checkInViewController 添加到 rootTabBarController 或 rootViewController 属性窗口的,所以它在空气中(只是被当前对象保留)。当这个(当前)对象被释放但 checkInViewController 的视图保持在窗口上(保留)时会发生什么?
如果您释放 checkInViewController 但其视图仍由窗口保留,则可能会产生一些问题...
关于该错误,我认为在某个地方对您的对象存在弱引用(未保留),该引用在对象被删除后对其起作用释放了。
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?
If you release your checkInViewController but its view is still retained by the window, it is probably going to create some issues...
About the error, I think there's somewhere a weak reference (not retained) to your object that acts on it after it's freed.