Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App dies in [self swizzled_addSubview:subview]; #25

Open
ghost opened this issue Jan 17, 2013 · 27 comments
Open

App dies in [self swizzled_addSubview:subview]; #25

ghost opened this issue Jan 17, 2013 · 27 comments

Comments

@ghost
Copy link

ghost commented Jan 17, 2013

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

@gpoitch
Copy link

gpoitch commented Jan 31, 2013

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

@kylef
Copy link
Contributor

kylef commented Feb 7, 2013

5352d1f has resolved this issue.

@runmad
Copy link

runmad commented Mar 6, 2013

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

@emersonmalca
Copy link

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

@danielamitay
Copy link
Owner

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

@echoz
Copy link
Contributor

echoz commented Mar 23, 2013

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).

@emersonmalca
Copy link

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

@kylef
Copy link
Contributor

kylef commented May 5, 2013

@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

@emersonmalca
Copy link

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];
*/

@danielamitay
Copy link
Owner

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.

@bobspryn
Copy link

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

@z-br
Copy link

z-br commented Jan 10, 2014

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

@ErwannRobin
Copy link

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!

@jschuster
Copy link

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

@abhishekkharwar
Copy link

Thanks

+- (void)dealloc {

  • [[self view] removeKeyboardControl];
    +}

Worked for me :)

@iteshapptree
Copy link

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

@joshuafeldman
Copy link

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
Copy link

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.

@jlukanta
Copy link

@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.

@Ashleyjwsi
Copy link

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

@joshuafeldman
Copy link

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.

@mfarhand
Copy link

mfarhand commented Oct 3, 2015

@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
Copy link

is there any respond to my question ?

@joshuafeldman
Copy link

@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

@mfarhand
Copy link

@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

@joshuafeldman
Copy link

@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

@mfarhand
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests