DAKeyboardControl icon indicating copy to clipboard operation
DAKeyboardControl copied to clipboard

App dies in [self swizzled_addSubview:subview];

Open ghost opened this issue 12 years ago • 27 comments

Hi there,

I was trying to use DAKeyboardControl, but my App crashes after the second load of DAKeyboardControl. To show what I mean, I modified the Example project. You can find it here: https://github.com/appcominteractive/DAKeyboardControl/tree/swizzeledError

To see the crash: push the new push button, go back, and press the push button again. Tested with Xcode 4.5.2 on iPhone 5.0 and iPhone 6.0 Simulator.

Regards, Christian

ghost avatar Jan 17 '13 13:01 ghost

+1 Seeing this come in on my live app's crash logs quite often.

gpoitch avatar Jan 31 '13 03:01 gpoitch

5352d1ff has resolved this issue.

kylef avatar Feb 07 '13 10:02 kylef

It's still an issue for me, even with the changes from 5352d1f

runmad avatar Mar 06 '13 17:03 runmad

Same here, for me it happened when I was presenting a modal view controller from a navigation controller. And the view was of class: UINavigationTransitionView and subview to be added was of class: UIViewControllerWrapperView

emersonmalca avatar Mar 09 '13 22:03 emersonmalca

Per @kylef, 5352d1ff fixed @appcominteractive 's example. Can one of you guys provide a reproducible demo and post it here?

danielamitay avatar Mar 09 '13 23:03 danielamitay

Using 2.1.0 and getting crashes after loading DAKeyboardControl the second time.

Getting messages being sent to deallocated instances.

*** -[UIView inputKeyboardWillChangeFrame:]: message sent to deallocated instance 0x1f3a1750

Hacky fix by calling removeKeyboardControl just before the view disappears and only if the view controller has been removed from the navigation controller's stack (ie. popped off the stack).

echoz avatar Mar 23 '13 04:03 echoz

Reproducible demo of the crash: https://github.com/emersonmalca/DAKeyboardControl

emersonmalca avatar May 05 '13 03:05 emersonmalca

@emersonmalca, you need to remove keyboard control when your view goes away. This isn't an issue with DAKeyboardControl.

https://github.com/danielamitay/DAKeyboardControl#remove-the-nsnotification-observer-at-the-end-of-a-vcs-life-convenience-method

diff --git a/DAKeyboardControlExample/DAKeyboardControlExample/ViewController.m b/DAKeyboardControlExample/DAKeyboardControlExample/ViewController.m
index 3042579..8078d6a 100644
--- a/DAKeyboardControlExample/DAKeyboardControlExample/ViewController.m
+++ b/DAKeyboardControlExample/DAKeyboardControlExample/ViewController.m
@@ -97,4 +97,8 @@
     [self dismissViewControllerAnimated:YES completion:NULL];
 }

+- (void)dealloc {
+    [[self view] removeKeyboardControl];
+}
+
 @end

kylef avatar May 05 '13 13:05 kylef

thanks @kylef, so the comment in the demo in the action handler block is no valid anymore then, since calling that method is not optional:

/* Try not to call "self" inside this block (retain cycle). But if you do, make sure to remove DAKeyboardControl when you are done with the view controller by calling: [self.view removeKeyboardControl]; */

emersonmalca avatar May 05 '13 13:05 emersonmalca

Yeah I should make it more explicit that DAKeyboardControl really should be balanced by calling -(void) removeKeyboardControl. I would certainly like to find a hook that can do this automatically, but haven't found a good way yet.

Of course, at the same time I would like to hunt down exactly why not removing DAKeyboardControl is causing issues with the method swizzling, as I currently see no obvious reason why... =(

But yes, balancing the calls will fix this issue. Thanks @emersonmalca for the great demo, and @kylef again.

danielamitay avatar May 05 '13 17:05 danielamitay

Was also confused as that comment makes it look optional (only required if you have a retain cycle?) Figured that wasn't the case. :)

bobspryn avatar May 30 '13 06:05 bobspryn

Any updated here? We really want to use this control but it crashes in IOS7

z-br avatar Jan 10 '14 07:01 z-br

same for me. I got a few crashes on my live app, and I have managed to reproduce this on app screens that doesn't event need DAKeyboardControl ... very useful control anyway!

ErwannRobin avatar Aug 05 '14 10:08 ErwannRobin

Same thing here. Also see a lot of crash reports related to that issue. Stacktrace like this:

1    libobjc.A.dylib     (anonymous namespace)::AutoreleasePoolPage::autoreleaseSlow(objc_object*) + 430
2    libsystem_malloc.dylib  szone_malloc_should_clear + 2766
3    libobjc.A.dylib     objc_object::rootAutorelease2() + 52
4    UIKit   -[UIView(Hierarchy) subviews] + 78
5    UIKit   -[UIView(Internal) _recursiveNotifyInteractionTintColorDidChangeForReasons:] + 188
…
12   MYAPP   -[UIView(DAKeyboardControl) swizzled_addSubview:] (DAKeyboardControl.m:577)
13   UIKit   -[UITableViewCellLayoutManager layoutSubviewsOfCell:] + 1640
...
22   UIKit   __66-[UISectionRowData refreshWithSection:tableView:tableViewRowData:]_block_invoke + 448

jschuster avatar Sep 12 '14 09:09 jschuster

Thanks

+- (void)dealloc {

  • [[self view] removeKeyboardControl]; +}

Worked for me :)

abhishekkharwar avatar Sep 22 '14 11:09 abhishekkharwar

It is happening for ios 7 and ios 8.

I have tried with this also. It did not work out. App is getting crashed when I receive push notification and I have re intialize the root view and go the the main class using push view. Sometimes it is causing crash

(void)dealloc {

[[self view] removeKeyboardControl]; }

is there any way that swizzled_addSubview will not be called it instance is deallocated

iteshapptree avatar Nov 19 '14 12:11 iteshapptree

We see this crash in our application as well. We do indeed call removeKeyboardControl and the crash still happens.

Fatal Exception: NSInvalidArgumentException Can't add self as subview

joshuafeldman avatar Dec 17 '14 18:12 joshuafeldman

I am suspecting that this crash is not related to DAKeyboardControl but rather an unrelated bug. I think the only reason it is showing up this way is because addSubview is swizzled. I have seen some weirdness with UINavigationController where the same view is presented twice. I think it is an iOS bug because I have seen it in many applications. I believe this is the crash people are seeing.

Of course I could be wrong, but this is what our research came up with.

joshuafeldman avatar Dec 19 '14 18:12 joshuafeldman

@joshuafeldman your suspicion is correct in my case. The crash is indirectly triggered by another bug in a different part of the code. Basically, my view shouldn't have been deallocated, and calling removeKeyboardControl on this view crashed the app.

jlukanta avatar Dec 31 '14 09:12 jlukanta

Hi My app is crashing for the same issue. I have write [self.view removeKeyboardControl] but still getting crash. Kindly help.

Ashleyjwsi avatar Jul 21 '15 04:07 Ashleyjwsi

Finally figured this one out, and it was really difficult. It seems like some of the views in the Apple keyboard window don't actually respond to UIResponder properties and methods (no idea how this is possible, Apple does some crazy internal stuff). Pretty sure it is an Apple bug.

What we did to work around the bug is create a category on UIResponder and swizzled the properties / methods that get accessed and wrapped the call to the original method inside of a respondsToSelector: and it seems to have worked nicely.

joshuafeldman avatar Sep 03 '15 01:09 joshuafeldman

@joshuafeldman i can't understand completely your last comment. can you make conclusion for this issue ? Finally can you give us a sample about you solution category on UIResponder ? but i think my problem a little different this is my stack trace in Fabric. this method have called over 500 time !!! http://uupload.ir/files/56g_screen_shot_2015-10-03_at_3.25.04_pm.png

mfarhand avatar Oct 03 '15 08:10 mfarhand

is there any respond to my question ?

mfarhand avatar Oct 13 '15 07:10 mfarhand

@mfarhand we ended up removing DAKeyboardControl since iOS 7+ supports the pan gesture to dismiss the keyboard without requiring this library.

But while we had it implemented we used something like this to prevent that bug where a weird subclass of UIView actually doesn't respond to the isFirstResponder method.

-swizzleInstanceMethod:toMethod: is a category method on NSObject that I didn't include in the snippet below.

@implementation UIResponder (KeyboardViewResponderFix)

+ (void)load
{
    SEL originalSelector = @selector(isFirstResponder);
    SEL swizzledSelector = @selector(swizzled_isFirstResponder);

    [self swizzleInstanceMethod:swizzledSelector toMethod:originalSelector];
}

- (BOOL)swizzled_isFirstResponder
{
    if ( [self respondsToSelector:@selector(swizzled_isFirstResponder)] ) {
        return [self swizzled_isFirstResponder];
    }

    return NO;
}

@end

joshuafeldman avatar Oct 13 '15 16:10 joshuafeldman

@joshuafeldman thanks for your reply i have 2 question :

  1. i can't understand your mean about : -swizzleInstanceMethod:toMethod: is a category method on NSObject that I didn't include in the snippet below. is it enough if create a category from NSObject & swizling like your code or i need extra code ?

what is the best solution for handle without this library?(we we to set out table view on top of keyboard frame & dismiss it with panning gesture

mfarhand avatar Oct 13 '15 20:10 mfarhand

@mfarhand yes if you have your own method to swizzle just use it there. If your applications supports iOS 7+ I highly suggest looking at https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/#//apple_ref/occ/instp/UIScrollView/keyboardDismissMode

joshuafeldman avatar Oct 13 '15 21:10 joshuafeldman

@joshuafeldman tnx for your help , i will try it and notify you about this issue.

mfarhand avatar Oct 14 '15 11:10 mfarhand