简体   繁体   中英

Thread 1:EXC_BAD_ACCESS code = 1 float *

I know that there are a tons of question with same question, but no one helped me.

I have Object DimensionItemHolder and property :

@property(nonatomic) float *  sum;

in recursion function I have :

DimensionItemHolder * holder = [self getDimensionItemWhitIndex:[dimensionItem.dimItemID shortValue]];

if (holder)
{

    NSLog(@"%f , %f , %f , %f , %f  : %p", holder.sum[0],holder.sum[1],holder.sum[2],holder.sum[3],holder.sum[4] , holder.sum);

    [self.itemOrderdArray removeObject:holder];
    [self substractSum:holder.sum];
    NSLog(@"%p", holder.sum);
    return holder.sum;
}

And after few iteration xcode throw

Thread 1: EXC_BAD_ACCESS(code = 1, address 0xc080000c) 

at line

NSLog(@"%p", holder.sum);

I figer it out that it has to do whit function

[self substractSum:holder.sum];

(if I comment it it works)

the function is :

-(void)substractSum:(float *)subSum{
if (subSum) {
    self.sum[0] = self.sum[0] - subSum[0];
    self.sum[1] = self.sum[1] - subSum[1];
    self.sum[2] = self.sum[2] - subSum[2];
    self.sum[3] = self.sum[3] - subSum[3];
    self.sum[4] = self.sum[4] - subSum[4];
}

}

I guess you're using float * to enjoy C pointer characteristics and do sum[i] . But here you are in objectiveC, use NSArray filled with floats.

I finally figure it out.

It was stupid thing. I malloc memory for 4 floats and I ask him for holder[4] which is 5 element.

Thanks all for help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM