-2

I am facing such strange issue right now, I am trying to implement calculator in my project the demo. The place I want to implement is ARC enable and my project ARC disable everything is working perfectly in that demo its working perfect in my project tOO but when i try to do operation on float values then my application crashes says EXC_BAD_ACCESS(code 1.... below is my code

_currentValue and _previousValue are like this in .h file

@property (retain,nonatomic) NSNumber* currentValue;  
@property (retain,nonatomic) NSNumber* previousValue;

in my .m file there are 2 methods where i face problem

- (NSString *)calculateValueToString:(NSString *)valueString ForType:(enum State)type{

    _currentValue = [numberFormatterFormal numberFromString:valueString];

    NSLog(@"%@",_currentValue);  //whatever number i input it get prints here

    [self calculateValue]; // this method get called
    state = type;          

    if (state != Equal){
        _previousValue = _currentValue;
         NSLog(@"%@",_previousValue);  // get print 
        _currentValue = @0 ;
    }

     NSLog(@"_previousValue%@",_previousValue);   // get print 
    NSLog(@"_currentValue%@",_currentValue);      // get print
    return [numberFormatterFormal stringFromNumber:_currentValue]; 
}

- (void)calculateValue{ 

    switch (state) { 
        case Plus:
            _currentValue = [NSNumber numberWithDouble:[_previousValue doubleValue] + [_currentValue doubleValue]];
            break;
        case Minus:                        //GET ONLY EXECUTE ONLY IF OPERATION IS -

              NSLog(@"%@",_currentValue);  // it has value 

   --->>>>>>> HERE APP CRASH         NSLog(@"%@",_previousValue);   // app crashes here

            _currentValue = [NSNumber numberWithDouble:[_previousValue doubleValue] - [_currentValue doubleValue]];

               NSLog(@"%@",_currentValue);  
              // THIS ALL WORK PERFECTLY IN THAT DEMO WHICH IS ARC ENABLE 
            break;
        case Multiple:
            _currentValue = [NSNumber numberWithDouble:[_previousValue doubleValue] * [_currentValue doubleValue]];
            break;
        case Divide:
            _currentValue = [NSNumber numberWithDouble:[_previousValue doubleValue] / [_currentValue doubleValue]];
            break;
        default:
            break;
    }
}
Prakash Desai
  • 511
  • 3
  • 14

1 Answers1

1

You're accessing your instance variables directly, and using manual reference counting. This isn't going to work.

All of the memory management is done in the synthesised accessor methods, so you must use the property accessors, e.g:

self.currentValue = ....;

Never

_currentValue = ...;

Or you will be bypassing all of the retain / release calls that the accessors are doing for you.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • but some where i heard that in ios we dont need to synthesis property we can use _currentvalue directly correct me please if i going on wrong direction – Prakash Desai Aug 10 '13 at 09:17
  • And sir one more question everything is working perfectly but problem only arise when operation is minus and values are float why so? why am i not facing problem in my every operation? – Prakash Desai Aug 10 '13 at 09:25
  • You don't need to synthesize properties, and the ivar is created for you, but if you're using manual reference counting you _must_ use the property accessors. As for your second question, probably due to the fact that NSNumber uses singletons for low value integers, so you don't have a releasing problem. – jrturton Aug 10 '13 at 13:55
  • i understand sir but then why its not working only for float values and only when i do operation of minus? except that everything works fine please make me understand i am so confuse – Prakash Desai Aug 10 '13 at 13:57
  • right now i need to go sir but i will reply you whenever i will come back thank you so much for explanation and please tell me why its working for other operation there also i am using _currentvalue and _previous values its so strange that i am not facing problem there as you have said i know you will clear my doubt – Prakash Desai Aug 10 '13 at 14:00
  • I can't really explain any further than I did in the second part of my last comment, and that's just a guess. – jrturton Aug 10 '13 at 14:00