0

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];
}

}

Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97

2 Answers2

0

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.

shinyuX
  • 1,519
  • 1
  • 9
  • 20
  • 1
    Yes that is the reason of float *. I use it because of performance issue. But there is no reason why should use NSArray for this bug – Marko Zadravec Dec 02 '13 at 10:05
  • 1
    An `NSArray` object can't hold floats. You'd need to use `NSNumber`s with float values. Same same but different :) – Guy Kogus Dec 02 '13 at 10:05
  • as Guy Kongus say, nsarray needs objects and those object must be created as NSArray and that needs time. If you fill 500 000 data in the data set there is a diference in a 4 min of using NSArray or float *. But this stil dont answer my question. – Marko Zadravec Dec 02 '13 at 10:12
0

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

Marko Zadravec
  • 8,298
  • 10
  • 55
  • 97