0

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
ankit_rck
  • 1,796
  • 2
  • 14
  • 24
  • NSMutableArray is not thread safe so this is no big surprise. See e.g. https://stackoverflow.com/questions/12098011/objective-c-is-nsmutablearray-thread-safe – rist Jan 22 '14 at 14:02
  • @rist The array isn't being corrupted; the objects within it are. In fact this issue has little to do with `NSMutableArray` at all, by the look of it. – trojanfoe Jan 22 '14 at 14:08
  • OP: You'll need to show the code that populates the array and creates those `UnitCost` objects. – trojanfoe Jan 22 '14 at 14:08
  • @trojanfoe please find the edited question i have added the code to populate the NSmutableArray – ankit_rck Jan 22 '14 at 15:20

2 Answers2

3

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.

ankit_rck
  • 1,796
  • 2
  • 14
  • 24
0

use NSArray * and copy.

NSArray *chart_data = [[HttpMethods GetChartDataPost:1] copy];
Dávid Kaszás
  • 1,877
  • 1
  • 16
  • 20
  • I am still getting the same error, random values, sometimes even vague values get assigned to the variables inside the array. – ankit_rck Jan 23 '14 at 05:22