xcode IOS 5 消息通过 alloc 命令发送到解除分配的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9315815/
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
IOS 5 message sent to deallocated instance on alloc command
提问by Patrik
I have the following error on a Ios 5 application using ARC:
我在使用 ARC 的 Ios 5 应用程序上出现以下错误:
*** -[ViewDettaglio respondsToSelector:]: message sent to deallocated instance 0x12193300
on console i write command:
在控制台上我写命令:
info malloc-history 0x12193300
and i get the following stack-trace:
我得到以下堆栈跟踪:
Alloc: Block address: 0x12193300 length: 192 Stack - pthread: 0xa08a3540 number of frames: 31 0: 0x96bdab03 in malloc_zone_calloc 1: 0x96bdaa5a in calloc 2: 0x16f8c93 in class_createInstance 3: 0x170388b in _objc_rootAllocWithZone 4: 0x21af661 in +[NSObject allocWithZone:] 5: 0x17038b9 in _objc_rootAlloc 6: 0x2c4c8 in -[ViewElenco CaricaViewDettaglio:] at /Users/.../ViewElenco.m:186 7: 0x2e550 in -[ViewElenco mapView:annotationView:calloutAccessoryControlTapped:] at /Users/.../ViewElenco.m:337 8: 0x3fa99c 9: 0x405faa in MKLongHash 10: 0x21aeec9 in -[NSObject performSelector:withObject:withObject:] 11: 0x60d5c2 in -[UIApplication sendAction:to:from:forEvent:] 12: 0x60d55a in -[UIApplication sendAction:toTarget:fromSender:forEvent:] 13: 0x6b2b76 in -[UIControl sendAction:to:forEvent:] 14: 0x6b303f in -[UIControl(Internal) _sendActionsForEvents:withEvent:] 15: 0x6b22fe in -[UIControl touchesEnded:withEvent:] 16: 0x632a30 in -[UIWindow _sendTouchesForEvent:] 17: 0x632c56 in -[UIWindow sendEvent:] 18: 0x619384 in -[UIApplication sendEvent:] 19: 0x60caa9 in _UIApplicationHandleEvent 20: 0x1a95fa9 in PurpleEventCallback 21: 0x21811c5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ 22: 0x20e6022 in __CFRunLoopDoSource1 23: 0x20e490a in __CFRunLoopRun 24: 0x20e3db4 in CFRunLoopRunSpecific 25: 0x20e3ccb in CFRunLoopRunInMode 26: 0x1a94879 in GSEventRunModal 27: 0x1a9493e in GSEventRun 28: 0x60aa9b in UIApplicationMain 29: 0x20bb in main at /Users/.../main.m:14 30: 0x2065 in start
The code at line 186 of ViewElenco.m is the following:
ViewElenco.m 第 186 行的代码如下:
ViewDettaglio *viewq=[[ViewDettaglio alloc] initWithNibName:@"ViewDettaglio" bundle:nil];
How can this happen? I'm using a UINavigationController to navigate from ViewElenco and ViewDettaglio.
这怎么会发生?我正在使用 UINavigationController 从 ViewElenco 和 ViewDettaglio 进行导航。
EDIT
编辑
Is it possibile that in the following code:
是否有可能在以下代码中:
ViewDettaglio* viewDettaglio=[[ViewDettaglio alloc] initWithNibName:@"ViewDettaglio" bundle:nil]; viewDettaglio.idObject=idObj; [self.navigationController pushViewController:viewDettaglio animated:YES];
alloc returns a deallocated object?
alloc 返回一个释放的对象?
回答by Patrik
The problem has been solved: in ViewDettaglio and ViewElenco there was a MKMapView, and the delegate was set to the container ViewController. When pushing a new ViewController in the UINavigationController, probably some thread created by MapView was still running and calling map delegate, even if it was not visible.
问题已经解决了:在ViewDettaglio和ViewElenco中有一个MKMapView,并且委托被设置到了容器ViewController。在 UINavigationController 中推送新的 ViewController 时,可能某些由 MapView 创建的线程仍在运行并调用地图委托,即使它不可见。
The solution was to set delegate to null when view will disapper, and set it again before view will appear:
解决方案是在视图消失时将委托设置为空,并在视图出现之前再次设置它:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.mapView.delegate=nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.mapView.delegate=self;
}