0

I found a strange bug in my code, but I can't reproduce it whenever I want. Sometimes, my iPad app crashes with the following keypath error :

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}";
}

Here is the code that handle the keypath observation :

-(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];
}    

and here is where I attach it :

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

'selected' being of UIObject type.

I don't use UIWebLayer anywhere in my code. Why is there an objet mutation from UITextView to UIWebLayer ? What am I doing wring ?

Diwann
  • 888
  • 1
  • 8
  • 28
  • Did you forget to unregister your textview in its dealloc? – borrrden Aug 24 '12 at 09:01
  • my textview is not really unregister as I am using ARC. When I don't need it anymore, I simply do : [tv removeFromSuperview]; tv = nil; Instruments hasn't detected any zombies, but I guess that if the strange textview is still alive somewhere, it's not yet a zombie – Diwann Aug 24 '12 at 10:45

2 Answers2

0

Try this:

In your .h file i.e @interface file, declare UITextView *tv; then where ever required write this code.

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

This method will always help you stay away from getting stuck in any memory corruption issues. I just hope it does the trick for you. Good Luck!

Apple_iOS0304
  • 1,092
  • 1
  • 9
  • 19
  • I am using ARC and cannot manually release objects. However I assigned it to nil before allocating it again, hope it can help – Diwann Aug 24 '12 at 10:33
0

I think I may have found the solution with this post : https://stackoverflow.com/a/4134583/1128754

I added "removeObserver" before "removeFromSuperview".

    [tv removeObserver:self forKeyPath:@"contentSize"];
Community
  • 1
  • 1
Diwann
  • 888
  • 1
  • 8
  • 28