Search code examples
xcodeios5gdbinstance

IOS 5 message sent to deallocated instance on alloc command


I have the following error on a Ios 5 application using ARC:


    *** -[ViewDettaglio respondsToSelector:]: message sent to deallocated instance 0x12193300

on console i write command:


    info malloc-history 0x12193300

and i get the following stack-trace:


    Alloc: Block address: 0x12193300 length: 192
    Stack - pthread: 0xa08a3540 number of frames: 31
        0: 0x96bdab03 in malloc_zone_calloc
        1: 0x96bdaa5a in calloc
        2: 0x16f8c93 in class_createInstance
        3: 0x170388b in _objc_rootAllocWithZone
        4: 0x21af661 in +[NSObject allocWithZone:]
        5: 0x17038b9 in _objc_rootAlloc
        6: 0x2c4c8 in -[ViewElenco CaricaViewDettaglio:] at /Users/.../ViewElenco.m:186
        7: 0x2e550 in -[ViewElenco mapView:annotationView:calloutAccessoryControlTapped:] at /Users/.../ViewElenco.m:337
        8: 0x3fa99c
        9: 0x405faa in MKLongHash
       10: 0x21aeec9 in -[NSObject performSelector:withObject:withObject:]
       11: 0x60d5c2 in -[UIApplication sendAction:to:from:forEvent:]
       12: 0x60d55a in -[UIApplication sendAction:toTarget:fromSender:forEvent:]
       13: 0x6b2b76 in -[UIControl sendAction:to:forEvent:]
       14: 0x6b303f in -[UIControl(Internal) _sendActionsForEvents:withEvent:]
       15: 0x6b22fe in -[UIControl touchesEnded:withEvent:]
       16: 0x632a30 in -[UIWindow _sendTouchesForEvent:]
       17: 0x632c56 in -[UIWindow sendEvent:]
       18: 0x619384 in -[UIApplication sendEvent:]
       19: 0x60caa9 in _UIApplicationHandleEvent
       20: 0x1a95fa9 in PurpleEventCallback
       21: 0x21811c5 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
       22: 0x20e6022 in __CFRunLoopDoSource1
       23: 0x20e490a in __CFRunLoopRun
       24: 0x20e3db4 in CFRunLoopRunSpecific
       25: 0x20e3ccb in CFRunLoopRunInMode
       26: 0x1a94879 in GSEventRunModal
       27: 0x1a9493e in GSEventRun
       28: 0x60aa9b in UIApplicationMain
       29: 0x20bb in main at /Users/.../main.m:14
       30: 0x2065 in start

The code at line 186 of ViewElenco.m is the following:


    ViewDettaglio *viewq=[[ViewDettaglio alloc] initWithNibName:@"ViewDettaglio" bundle:nil];

How can this happen? I'm using a UINavigationController to navigate from ViewElenco and ViewDettaglio.

EDIT

Is it possibile that in the following code:


    ViewDettaglio* viewDettaglio=[[ViewDettaglio alloc] initWithNibName:@"ViewDettaglio" bundle:nil];
    viewDettaglio.idObject=idObj;
    [self.navigationController pushViewController:viewDettaglio animated:YES];

alloc returns a deallocated object?


Solution

  • The problem has been solved: in ViewDettaglio and ViewElenco there was a MKMapView, and the delegate was set to the container ViewController. When pushing a new ViewController in the UINavigationController, probably some thread created by MapView was still running and calling map delegate, even if it was not visible.

    The solution was to set delegate to null when view will disapper, and set it again before view will appear:

    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        self.mapView.delegate=nil;
    }
    - (void)viewWillAppear:(BOOL)animated
    {    
        [super viewWillAppear:animated];
        self.mapView.delegate=self;
    }