ios:crashValueForKeyPath后崩溃

问题描述:

我在我的代码中发现了一个奇怪的错误,但无法随时随地重现。 有时,下面的keyPath错误我的iPad应用程序崩溃:ios:crashValueForKeyPath后崩溃

Keypath contentSize changed 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<UIWebDocumentView: 0x1c9fce00; frame = (0 0; 1034 75); text = 'coucou'; opaque = NO; userInteractionEnabled = NO; layer = <UIWebLayer: 0x1daa9760>>: An -observeValueForKeyPath:ofObject:change:context: message was received but not handled. 
Key path: contentSize 
Observed object: <UITextView: 0x1da83e60; frame = (32 32; 1034 198); text = 'coucou'; layer = <CALayer: 0x1dad2650>; contentOffset: {0, -62}> 
Change: { 
kind = 1; 
new = "NSSize: {1034, 75}"; 
} 

这里是处理的keyPath观察代码:

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
if (object != textViewCurrentlyEditing) 
    return; 
NSLog(@"Keypath %@ changed", keyPath); 
UITextView *tv = object; 
UIObject *selected = (__bridge UIObject *)context; 
[self updateTextViewAlign:tv forObject:selected]; 
}  

和这里就是我将其附加:

UITextView *tv = [[UITextView alloc] initWithFrame:button.frame]; 
[tv addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:(void *)selected]; 
textViewCurrentlyEditing = tv; 

'selected'是UIObject类型。

我不在我的代码中的任何地方使用UIWebLayer。 为什么从UITextView到UIWebLayer有objet变异? 我在干什么?

+0

你忘了在其dealloc中注销你的textview吗? – borrrden

+0

我的textview并没有真正取消注册,因为我正在使用ARC。 当我不再需要它时,我只需要: [tv removeFromSuperview]; tv = nil; 仪器没有检测到任何僵尸,但我猜如果奇怪的textview仍然在某处活着,它还不是僵尸 – Diwann

我想我可能已经发现了这个职位的解决方案:https://*.com/a/4134583/1128754

我补充说:“removeObserver”“removeFromSuperview”之前。

[tv removeObserver:self forKeyPath:@"contentSize"]; 

试试这个:

在您的.h文件中即@interface文件,申报的UITextView *电视; 然后凡有需要写这段代码。

if(tv)[tv release]; 
tv = [[UITextView alloc] initWithFrame:button.frame]; 

此方法将始终帮助您避免陷入任何内存损坏问题。 我只是希望它能为你解决问题。祝你好运!

+0

我正在使用ARC并且无法手动释放对象。 但是我在再次分配之前将它分配给了零,希望它能帮助 – Diwann