【问题标题】:ios : crash after observeValueForKeyPathios:observeValueForKeyPath 后崩溃
【发布时间】:2012-08-24 08:58:35
【问题描述】:

我在我的代码中发现了一个奇怪的错误,但我无法随时重现它。 有时,我的 iPad 应用程序因以下键路径错误而崩溃:

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 的对象突变? 我在做什么?

【问题讨论】:

  • 您是否忘记在其 dealloc 中取消注册您的 textview?
  • 我的 textview 并没有真正取消注册,因为我正在使用 ARC。当我不再需要它时,我只需:[tv removeFromSuperview];电视=无; Instruments 没有检测到任何僵尸,但我猜如果奇怪的 textview 在某个地方还活着,它还不是僵尸

标签: ios ipad


【解决方案1】:

试试这个:

在您的 .h 文件(即@interface 文件)中,声明 UITextView *tv; 然后在需要的地方编写此代码。

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

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

【讨论】:

  • 我正在使用 ARC,无法手动释放对象。但是我在再次分配之前将它分配给了 nil,希望它可以帮助
【解决方案2】:

我想我可能已经通过这篇文章找到了解决方案:https://stackoverflow.com/a/4134583/1128754

我在“removeFromSuperview”之前添加了“removeObserver”。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2020-03-15
    相关资源
    最近更新 更多