Nsmutable Array getting corrupt on accessing it

175 Views Asked by At

I have a NSMutable array that gets populated from an api call and has 31 valid values in it.

enter image description here

As we can see It has a collection of class object which has 4 attributes in it. Each of them have valid values assigned to them. Eg: dat,ancillary etc. This is in dispatch_aync.

In the main queue, i am traversing the chart_data but i EXC_BAD_ACCESS code=1, when i tried to debug deeper into the matter. I found that my NSmutable array was getting corrupt.

Here is a screenshot of when i access the array.

enter image description here

Random address or values are assigned to it which then breaks my code, because i expect double and date values.

There are no operations on the chart_data in between these 2 transactions. Any idea why this could be happening. and this started to happen out of a sudden where the code was working perfectly fine till today.

The code that popluates the Nsmutable array

+(NSMutableArray*)GetData:(NSDictionary*)dataArray
{        
    daily_unit_Array = [[NSMutableArray alloc]init];

    for (int i=0; i < 31; i++)
    {
        UnitCost *unit_cost = [[UnitCost alloc]init];

        unit_cost.date = [keys objectAtIndex:i];

        NSArray *unit_cost_value = [dataArray objectForKey:[keys objectAtIndex:i]];

        unit_cost.val1 = [unit_cost_value valueForKey:@"val1"];
        unit_cost.val2 = [unit_cost_value valueForKey:@"val2"];
        unit_cost.val3 = [unit_cost_value valueForKey:@"val3"];
        unit_cost.val4 = [unit_cost_value valueForKey:@"val4"];

        [daily_unit_Array addObject:unit_cost];
    }
    return daily_unit_Array;
}

and this is returned to the function that calls it and assigns it to chart_data

P.S : This is a sporadic issue that occurs 8 in 10 times. Sometimes when i run the app, it perfectly runs and shows me what i want. Note: Just a note. I updated my Xcode to 5.1 beta 4 version yesterday. but i dont seem that this could cause an issue, because i tried running my same code in older version of Xcode and i could reproduce the error.

2

There are 2 best solutions below

0
On

I finally found the solution. As @trojafoe pointed, the issue was not in the NSmutable Array but in the objects within them. I figured out that the properties of UnitCost object were of type (assign,nonatomic). As soon as i changed them to (retain,nonatomic) the issue got resolved...

I guess the values were not been retained.

1
On

use NSArray * and copy.

NSArray *chart_data = [[HttpMethods GetChartDataPost:1] copy];