如何在LLDB下排查message sent to deallocated instance问题

转载:http://www.devdiv.com/home.php?mod=space&uid=50901&do=blog&id=50856

谁实话,找了好久,好兴奋。


在XCode的以前版本中,如果遇到了

[代码]c#/cpp/oc代码:

1 message sent to deallocated instance 0x6d564f0
我们可以使用info malloc-history 0x6d564f0来查看调用堆栈来查看崩溃发生的地方,这种方法这里不作阐述,大家自行百度。

在新的XCode里,调试器默认使用LLDB,我就讲一下如何在LLDB状态下定位到由于内存操作不当引起的Crash
首先我有一段会发生崩溃的代码:

[代码]c#/cpp/oc代码:

1 NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:themePathTmp];
2 if(themePath)
3   self.backgroundIV.image = [UIImage imageWithContentsOfFile:[themePath stringByAppendingPathComponent:@"mask_1.png"]];
4    
5 [themePath release];
学过内存管理的人都应该知道在这里themePath并没有被retain,所以如果写了release,那么必然会发生崩溃情况。首先我们需要对开发的环境变量进行设置

运行代码,出现下面的崩溃现象

下面我们打开“活动监视器”,找到我们对应的PID,我们的Target为HPThemeManager,只要找到HPThemeManager对应的PID就可以(HPThemeManager是在论坛里下载的,本来正在看代码,就直接拿他来作试验了)

现在,我们得到了两个主要的信息:
进程ID:50127
崩溃地址:0x6d564f0

我们打开终端,输入以下命令:

[代码]c#/cpp/oc代码:

1 sudo malloc_history 50127 0x6d564f0
结果显示为:

这样我们用能定位到这行代码

[代码]c#/cpp/oc代码:

1 NSString *themePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:themePathTmp];

对themePath进行排查,就找到了崩溃的罪魁祸首

[代码]c#/cpp/oc代码:

1 [themePath release];


阅读更多
换一批

内存泄漏 *** -[CFString retain]: message sent to deallocated instance 0x786f6350

11-19

今天调试程序时提示EXC_BAD_ACCESS,按照网上的教程rnhttp://www.cnblogs.com/85538649/archive/2011/11/16/2251310.htmlrnhttp://www.cnblogs.com/ygm900/p/3667800.htmlrn,在终端里查看到了如下的信息:rnrnALLOC 0x7b09d6a0-0x7b09d6e7 [size=72]: thread_322b000 |start | main | UIApplicationMain | -[UIApplication _run] | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSources0 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ | FBSSerialQueueRunLoopSourceHandler | -[FBSSerialQueue _performNextFromRunLoopSource] | -[FBSSerialQueue _performNext] | __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke | __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 | -[UIApplication workspaceDidEndTransaction:] | __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke3208 | -[UIApplication _runWithMainScene:transitionContext:completion:] | -[UIApplication _loadMainInterfaceFile] | -[UIApplication _loadMainStoryboardFileNamed:bundle:] | -[UIStoryboard instantiateInitialViewController] | -[UIStoryboard instantiateViewControllerWithIdentifier:] | -[UINib instantiateWithOwner:options:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | UINibDecoderDecodeObjectForValue | -[UIRuntimeConnection initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | -[UIClassSwapper initWithCoder:] | -[UITabBarController initWithCoder:] | -[UIViewController initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | UINibDecoderDecodeObjectForValue | -[UIClassSwapper initWithCoder:] | -[UIViewController initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | -[UITabBarItem initWithCoder:] | -[UINibDecoder decodeObjectForKey:] | UINibDecoderDecodeObjectForValue | -[UIImageNibPlaceholder initWithCoder:] | _UIKitGetApplicationAssetManager | dispatch_once | dispatch_once_f | _dispatch_client_callout | ___UIKitGetApplicationAssetManager_block_invoke | -[_UIAssetManager initWithName:inBundle:idiom:] | -[_UIAssetManager _initWithName:inBundle:idiom:lock:allowMissingCatalog:] | -[CUICatalog initWithName:fromBundle:error:] | +[CUIThemeFacet themeNamed:forBundleIdentifier:error:] | RunTimeThemeRefForBundleIdentifierAndName | PerformBlockWithThemeRefCache | dispatch_barrier_sync | dispatch_barrier_sync_f | _dispatch_barrier_sync_f_invoke | _dispatch_client_callout | __PerformBlockWithThemeRefCache_block_invoke_2 | __RunTimeThemeRefForBundleIdentifierAndName_block_invoke | -[CUIStructuredThemeStore initWithPath:] | -[CUICommonAssetStorage initWithPath:] | -[CUICommonAssetStorage initWithPath:forWriting:] | -[CUICommonAssetStorage _commonInitWithStorage:forWritting:] | objc_msgSend | _class_lookupMethodAndLoadCache3 | lookUpImpOrForward | log_and_fill_cache(objc_class*, void (*)(), objc_selector*, objc_object*, objc_class*) | cache_fill | cache_t::reallocate(unsigned short, unsigned short) | allocateBuckets(unsigned short) | calloc | malloc_zone_calloc rn----rnFREE 0x7b09d6a0-0x7b09d6e7 [size=72]: thread_322b000 |start | main | UIApplicationMain | -[UIApplication _run] | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSources0 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ | FBSSerialQueueRunLoopSourceHandler | -[FBSSerialQueue _performNextFromRunLoopSource] | -[FBSSerialQueue _performNext] | __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke | __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 | -[UIApplication workspaceDidEndTransaction:] | __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke3208 | -[UIApplication _runWithMainScene:transitionContext:completion:] | -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] | -[UIWindow makeKeyAndVisible] | -[UIWindow _orderFrontWithoutMakingKey] | -[UIWindow _setHidden:forced:] | -[UIWindow addRootViewControllerViewIfPossible] | -[UIView(Hierarchy) addSubview:] | -[UIView(Internal) _addSubview:positioned:relativeTo:] | -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] | -[UIViewController viewWillMoveToWindow:] | -[UIViewController __viewWillAppear:] | -[UIViewController _setViewAppearState:isAnimating:] | -[UITabBarController viewWillAppear:] | -[UITabBarController _selectDefaultViewControllerIfNecessaryWithAppearanceTransitions:] | +[UIView(Animation) performWithoutAnimation:] | __87-[UITabBarController _selectDefaultViewControllerIfNecessaryWithAppearanceTransitions:]_block_invoke | -[UITabBarController setSelectedViewController:] | -[UITabBarController _setSelectedViewController:] | -[UITabBarController transitionFromViewController:toViewController:] | -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] | -[UITabBarController _viewForViewController:] | -[UIViewController view] | -[UIViewController loadViewIfRequired] | -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] | -[productController viewDidLoad] | -[UITableView setDataSource:] | -[NSObject respondsToSelector:] | class_respondsToSelector_inst | lookUpImpOrNil | lookUpImpOrForward | _class_resolveMethod | _class_resolveInstanceMethod(objc_class*, objc_selector*, objc_object*) | lookUpImpOrNil | lookUpImpOrForward | cache_fill | cache_t::reallocate(unsigned short, unsigned short) | cache_collect | free rnrnALLOC 0x7b09d6d0-0x7b09d6df [size=16]: thread_322b000 |start | main | UIApplicationMain | -[UIApplication _run] | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun | __CFRunLoopDoSources0 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ | FBSSerialQueueRunLoopSourceHandler | -[FBSSerialQueue _performNextFromRunLoopSource] | -[FBSSerialQueue _performNext] | __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke | __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 | -[UIApplication workspaceDidEndTransaction:] | __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke3208 | -[UIApplication _runWithMainScene:transitionContext:completion:] | -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] | -[UIWindow makeKeyAndVisible] | -[UIWindow _orderFrontWithoutMakingKey] | -[UIWindow _setHidden:forced:] | -[UIWindow addRootViewControllerViewIfPossible] | -[UIView(Hierarchy) addSubview:] | -[UIView(Internal) _addSubview:positioned:relativeTo:] | -[UIView(Hierarchy) _willMoveToWindow:withAncestorView:] | -[UIViewController viewWillMoveToWindow:] | -[UIViewController __viewWillAppear:] | -[UIViewController _setViewAppearState:isAnimating:] | -[UITabBarController viewWillAppear:] | -[UITabBarController _selectDefaultViewControllerIfNecessaryWithAppearanceTransitions:] | +[UIView(Animation) performWithoutAnimation:] | __87-[UITabBarController _selectDefaultViewControllerIfNecessaryWithAppearanceTransitions:]_block_invoke | -[UITabBarController setSelectedViewController:] | -[UITabBarController _setSelectedViewController:] | -[UITabBarController transitionFromViewController:toViewController:] | -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] | -[UITabBarController _viewForViewController:] | -[UIViewController view] | -[UIViewController loadViewIfRequired] | -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] | -[productController viewDidLoad] | -[color=#FF6600][productController configurationFilter][/color] | +[NSDictionary(NSDictionary) dictionaryWithContentsOfURL:] | -[__NSPlaceholderDictionary initWithContentsOfURL:] | +[NSDictionary(NSDictionary) newWithContentsOf:immutable:] | +[NSPropertyListSerialization propertyListWithData:options:format:error:] | CFPropertyListCreateWithData | _CFPropertyListCreateWithData | __CFTryParseBinaryPlist | __CFBinaryPlistCreateObjectFiltered | __CFBinaryPlistCreateObjectFiltered | __CFBinaryPlistCreateObjectFiltered | __CFBinaryPlistCreateObjectFiltered | CFStringCreateWithBytes | __CFStringCreateImmutableFunnel3 | _CFRuntimeCreateInstance | CFAllocatorAllocate | __CFAllocatorSystemAllocate | malloc_zone_malloc rnrn在前述两篇文章里,得到的报错信息就一行,所以我也不知道该如何去定位错误。会不会是上面标示的颜色部分导致的错误呢?rn此函数如下,会不会是这里的问题?rnrn[code=objc]rn- (NSArray *)configurationFilter rn NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"filtercat" ofType:@"plist"];rn NSURL *plisturl = [NSURL fileURLWithPath:plistPath];rn NSDictionary *data = [NSDictionary dictionaryWithContentsOfURL:plisturl];rn rn FilterCategory *zsCategory = [[FilterCategory alloc] init];rn zsCategory.CategoryTitle = @"分类";rn zsCategory.CategoryID = 1;rnrn NSDictionary *zs = [data objectForKey:[NSString stringWithFormat:@"cat1"]];rn //NSLog(@"%@",zs);rn NSMutableArray *subItems = [[NSMutableArray alloc]init];rn for (int i = 1; i <= (unsigned long)zs.count; i++) rn NSDictionary *dic = [zs objectForKey:[NSString stringWithFormat:@"item%d",i]];rn FilterItem *item = [[FilterItem alloc] init];rn item.ItemTitle = [dic objectForKey:@"itemTitle"];rn item.ItemID = [dic objectForKey:@"itemID"];rn item.Category = zsCategory;rnrn [subItems addObject:item];rn rn zsCategory.Items = subItems;rn return @[zsCategory];rnrn[/code]rn如果错误在此,该如何改正?rnrn另外,此报错只在iPhone5以下的机器上报错,iPhone5s以上未提示错误。

没有更多推荐了,返回首页