Joakim Engstrom
Joakim Engstrom

Reputation: 6403

EXC_BAD_ACCESS code=2 error in Obj-C code with ARC

I recently converted some code to ARC, most of of the code I did not write myself, but right now I've been trying for hours to figure this out.

This code runs good when not on ARC, although it gives some potential leaks when analyzing.

When I run I get an exception on the third line: poi[i]=[MapPOI alloc], but my guess is that the other segment might also cause some trouble.

poi = (__strong MapPOI **)malloc(sizeof(MapPOI *) * dbsize);
for (int i = 0; i < dbsize; i++) {
    poi[i] = [MapPOI alloc];
}
poilayer = (__strong CALayer **)malloc( sizeof(CALayer *) * ( dbsize + (extraDB == nil ? 0 : [extraDB count]) ) );

if (extraDB != nil) {
    extraPOI = (__strong MapPOI **)malloc(sizeof(MapPOI *) * [extraDB count]);
    for (int i = 0; i < [extraDB count]; i++) {
        extraPOI[i] = [MapPOI alloc];
    }
}

Anybody got any ideas on why this might cause trouble?

Upvotes: 0

Views: 1886

Answers (1)

JeremyP
JeremyP

Reputation: 86651

Anybody got any ideas on why this might cause trouble?

Well you are failing to -init the MapPOI objects.

Also, you should probably be storing them in NSArrays (or NSMutableArrays).

Upvotes: 2

Related Questions