How to Fix NZombie in iOS

92 Views Asked by At

I'm developing an app in iOS

enter image description here

this application captures signatures and also redraws them back from a database...

The problem appeared when I added threads, for printing on the labels the biometric data on real time while the signature is being drawn, randomly it crashes, I mean sometimes it draws the signature complete, sometimes just 3/4 of the signature or not even a half of it....

Used a tool called Instruments trying to detect the NSZombies and this is what I got...

enter image description here

enter image description here

and looking in the code, this is the portion of code I found...

enter image description here

this is the piece of code where it crashes:

-(void)imprimeValoresFirmaEnTabla:(NSMutableArray *)valores{
    if ([valores count] == 3) {
        if ([valores objectAtIndex:0] != nil) self.pressureStrokeLabel.text = [valores objectAtIndex:0];
        if ([valores objectAtIndex:1] != nil) self.speedStrokeLabel.text    = [valores objectAtIndex:1];
        if ([valores objectAtIndex:2] != nil) self.locationStrokeLabel.text = [valores objectAtIndex:2];        
    }
}

it says that -[NSConcreteMutableAtributedString lenght] is the responsible of the Zombie

this is my code:

- (IBAction)drawSignature{

    [self clear]; // clear the canvas
    NSData  *datos  = [self desSerializarFirma:[self traerFirmadeBD]]; //unpack the registered signature and store it on the NSData object

    if (datos.length <= 1) // if there's no data stored on database
        [[[UIAlertView alloc]initWithTitle:@"Notification" message:@"For showing your signature, it is required to register it first, draw your signature on the canvas and tap the Save button" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];

    if (datos.length > 1) { // if is data stored on database

        //store the signature on the object
        self.firmaCompleta  = [NSKeyedUnarchiver unarchiveObjectWithData:datos];


        //this loop draws each point on the canvas
        for (int i = 0; i < [self.firmaCompleta.location count]; i++) {


            SmoothStroke *currentStroke = [canvasView getStrokeForTouchHash:[[self.firmaCompleta.touchHash objectAtIndex:i] intValue]];

            //this draws each point using the biometric info
            [canvasView addLineToAndRenderStroke:currentStroke
                                         toPoint:CGPointFromString([self.firmaCompleta.location objectAtIndex:i])
                                         toWidth:[canvasView widthForPressure:[[self.firmaCompleta.pressure objectAtIndex:i] intValue]]
                                         toColor:[canvasView colorForPressure:[[self.firmaCompleta.pressure objectAtIndex:i] intValue]]];



            //this stores the biometric data in an array for sending to background thread
            if (!valoresFirma)  valoresFirma = [[NSMutableArray alloc]init];
            [valoresFirma removeAllObjects];

            [valoresFirma addObject:[NSString stringWithFormat: @"%f", [canvasView widthForPressure:[[self.firmaCompleta.pressure objectAtIndex:i] intValue]]]];
            [valoresFirma addObject:[NSString stringWithFormat:@"%f",velocidadTrazo]];
            [valoresFirma addObject:[self.firmaCompleta.location objectAtIndex:i]];


            //this triggers the tread for printing the values on labels (real time)          
            [NSThread detachNewThreadSelector:@selector(imprimeValoresFirmaEnTabla:) toTarget:self withObject:valoresFirma];

        }
    }
}

and the code called by the thread:

-(void)imprimeValoresFirmaEnTabla:(NSMutableArray *)valores{
    if ([valores count] == 3) {
        if ([valores objectAtIndex:0] != nil) self.pressureStrokeLabel.text = [valores objectAtIndex:0];
        if ([valores objectAtIndex:1] != nil) self.speedStrokeLabel.text    = [valores objectAtIndex:1];
        if ([valores objectAtIndex:2] != nil) self.locationStrokeLabel.text = [valores objectAtIndex:2];        
    }
}

thanks in advance for your support

1

There are 1 best solutions below

0
jsd On

Don't do threading like that, it's too easy to shoot yourself in the foot. Create an NSOperation subclass to do the drawing. Make a custom initializer that takes an array. Store the array with the operation object so you know it won't be released on you. It's a few extra lines of code compared to detachNewThreadSelector but it's much safer. Plus you can cancel the operation if the user navigates away from the view that you're showing.

Read up on operations and operation queues here: http://developer.apple.com/library/mac/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008091