Unrecognized selector, only in simulator

Discussion in 'iOS Programming' started by idelovski, Oct 26, 2010.

  1. idelovski macrumors regular

    Sep 11, 2008
    I have a problem in simulator that started today and it just won't go away. My app crashes with NSInvalidArgumentException, unrecognized selector but the method exists and it all works just fine on device (both an iPhone and an iPod).

    Did everything I could - rebuild everything several times, erased my app from simulator, did "Reset content and settings" and nothing helped. I even brought several backups from yesterday and the day before, but nothing helped.

    *** -[NetworkController initWithMainViewController:]: unrecognized selector sent to instance 0x4c39e10
    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NetworkController initWithMainViewController:]: unrecognized selector sent to instance 0x4c39e10'
  2. amorya macrumors 6502

    Jun 17, 2007
    Could be a pesky memory management bug. Assuming it's always crashing on the same line of code, can you NSLog(@"%@", NetworkController); on the line before? That'll tell you whether the variable points to an instance of the class you think it does.

  3. idelovski thread starter macrumors regular

    Sep 11, 2008
    Just did NSlog() and to do that I broke up my init method into its parts. The init method that was crashing is not doing much so I'm calling now the regular -init.

       // NetworkController  *tmpNetController = [[NetworkController alloc] initWithMainViewController:self];
       NetworkController  *tmpNetController = [NetworkController alloc];
       NSLog (@"Ptr: %p, Obj: %@", tmpNetController, tmpNetController);
       tmpNetController = [tmpNetController init];
       tmpNetController.mainViewController = self;
    Ptr: 0x4c39df0, Obj: <NetworkController: 0x4c39df0>
    *** Assertion failure in -[NSThread performOneway:result:withTarget:selector:], /SourceCache/Message_Sim/Message-428.11/Utilities.subproj/MainThread.m:71
    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't find signature for target 0x4c39df0 selector _registerForNotificationKeys:patterns:'

    Then I moved that line before anything else in my application, in -viewDidLoad for the initial screen:

    - (void)viewDidLoad
       [super viewDidLoad];
       NetworkController  *tmpNetController = [[NetworkController alloc] initWithMainViewController:self];
    And it crashed with the same "unrecognized selector" message. On that main screen I have four other buttons that call other parts of the application and it all works just fine. The only thing that fails is this alloc/init for the NewtworkController and only in simulator.

    I think I'm in real trouble! ;)
  4. chown33 macrumors 604

    Aug 9, 2009
    The red-hilited "%@" causes tmpNetController's -description method to be invoked (read the reference doc for print formatting). Since the object has only been alloc'ed, but not yet init'ed, it is quite possible, or even quite likely, that the object is not yet in a valid condition to respond to -description.

    You don't show the superclass of of NetworkController, so there's no way to tell how badly it's going to respond, but it's almost certainly going to respond badly.
  5. idelovski thread starter macrumors regular

    Sep 11, 2008
    Well maybe, but NSLog() gives me the right name of the class, and most importantly, init method crashes even without that NSLog() call.

  6. JustSomeDude macrumors regular

    Apr 10, 2010
    Google reveals somebody having a similar problem because of an preexisting NetworkController class in the MessageUI framework (I'm guessing that you might be using this based on the assertion failure) - they fixed the issue by renaming their class.


    Edit: Although, I am curious why it only happens in the simulator since you would think you would still be creating the wrong object on the actual device(I am assume that is the actual issue in the simulator). Maybe conditional compile directives in the framework that are only applicable to the simulator in this case?
  7. idelovski thread starter macrumors regular

    Sep 11, 2008
    OMG, changed the name from NetworkController to NetworkingController now and all works ok. I was sure Apple always adds prefix to their stuff. I guess I was wrong.
  8. Thomas Harte macrumors 6502

    Nov 30, 2005
    They don't; we've also tripped over undocumented internal class names. What they do is strongly recommend that you use prefixes.
  9. chown33 macrumors 604

    Aug 9, 2009
    Maybe NetworkController only exists on the Simulator SDK. It doesn't have to be conditional compilation. There is no reason to expect that the code needed to run the simulator is 100% identical in name to code on the device. Remember that the simulator is a bunch of adapter classes layered on top of the existing Mac OS X classes, that make them appear to be something they aren't.

Share This Page