Search code examples
objective-cios4

Object is getting Deallocated after being Passed into Method


I have an application where when I pass an object into the method sometimes the object's value is changed to null. I could not find any thing like this in blogs, please suggest what I should do. I feel that is it like pass by reference or value but don't know much.

The variable declare in *.h file like

     NSNumber* resultSelectedWeekValue;

and property

     @property (nonatomic, retain) NSNumber* resultSelectedWeekValue;

and in implimentaion

    @synthesize resultSelectedWeekValue =       _resultSelectedWeekValue;

This is delegate method which get the value from one popover

// Show discription of Selected week
         -(void) getSelectedWeekDelegateMethod:(NSString*) getSelectedWeek {

        _resultSelectedWeekValue = [_dbObject getSelectedWeekValue:getSelectedWeek];

        NSLog(@"This is _tempOzs %@ ", _resultSelectedWeekValue);

        if ([_productAttributePopover isPopoverVisible]) {

          [_productAttributePopover dismissPopoverAnimated:YES];
       }
  }

      NSLog(@"This is _resultSelectedWeekValue %@ and class %@", _resultSelectedWeekValue, [_resultSelectedWeekValue class]);  // This say the object is deallocate? this is the code below

if ((_resultSelectedWeekValue != nil) && (_tempGranularProduct != nil || _tempLiquidProduct != nil) && ( _tempLbs != nil || _tempOzs != nil)) {

    if ([_tempLbs intValue] != 0) {

        NSDate* _createdDate = [NSDate date];
        //Create the dateformatter object
        NSDateFormatter* _formatter = [[[NSDateFormatter alloc] init] autorelease];
        //Set the required date format
        [_formatter setDateFormat:@"yyyy-MM-dd"];
        //Get the string date
        NSString* _formatDate = [_formatter stringFromDate:_createdDate];
        NSNumber* _getNumBags = [self calculateGranularBags];
        NSNumber* _getProductID = [[_dbObject getAppliedProductWeightAttribute:_tempGranularProduct] objectForKey:@"productid"];
        NSNumber* _totalInvestment = [[NSNumber alloc] initWithInt:0];


        NSLog(@"This is _tempScenarioId %@ and class %@", _tempScenarioId, [_tempScenarioId class]);
        NSLog(@"This is _resultSelectedWeekValue %@ and class %@", _resultSelectedWeekValue, [_resultSelectedWeekValue class]);
        NSLog(@"This is _getProductID %@ and class %@", _getProductID, [_getProductID class]);
        NSLog(@"This is _tempLbs %@ and class %@", _tempLbs, [_tempLbs class]);
        NSLog(@"This is _getNumBags %@ and class %@", _getNumBags, [_getNumBags class]);
        NSLog(@"This is _totalInvestment %@ and class %@", _totalInvestment, [_totalInvestment class]);
        NSLog(@"This is _tempCarryover %@ and class %@", _tempCarryover, [_tempCarryover class]);



        NSMutableDictionary* _scenarioProductAttribute = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                  _tempScenarioId, @"scenarioid",
                                                  _resultSelectedWeekValue, @"weekvalue",
                                                  _getProductID, @"productid",
                                                  _tempLbs, @"lbper1000ft",
                                                  _getNumBags, @"numberofbags",
                                                  _totalInvestment, @"totalinvestment",
                                                  _tempCarryover, @"carry_n",
                                                  _formatDate, @"date",
                                                  nil];
        [_totalInvestment release];

        _tempScenarioId = [_dbObject insertScenarioProductAttribute:_scenarioProductAttribute andScenarioId:_tempScenarioId andProductId:_getProductID andSelectedWeekValue:_resultSelectedWeekValue];

        // Check Point
        [TestFlight passCheckpoint:@"INSERT_GRANULAR_RECORD"];
    } 

    if ([_tempOzs intValue] != 0) {

        NSDate* _createdDate = [NSDate date];
        //Create the dateformatter object
        NSDateFormatter* _formatter = [[[NSDateFormatter alloc] init] autorelease];
        //Set the required date format
        [_formatter setDateFormat:@"yyyy-MM-dd"];
        //Get the string date
        NSString* _formatDate = [_formatter stringFromDate:_createdDate];
        NSNumber* _getGallons = [self calculateLiquidGallons];
        NSNumber* _getProductID = [[_dbObject getAppliedProductWeightAttribute:_tempLiquidProduct] objectForKey:@"productid"];
        NSNumber* _totalInvestment = [[NSNumber alloc] initWithInt:0];


        NSLog(@"This is _tempScenarioId %@ and class %@", _tempScenarioId, [_tempScenarioId class]);
        NSLog(@"This is _resultSelectedWeekValue %@ and class %@", _resultSelectedWeekValue, [_resultSelectedWeekValue class]);  // This say the object is deallocate?
        NSLog(@"This is _getProductID %@ and class %@", _getProductID, [_getProductID class]);
        NSLog(@"This is _tempOzs %@ and class %@", _tempOzs, [_tempOzs class]);
        NSLog(@"This is _getGallons %@ and class %@", _getGallons, [_getGallons class]);
        NSLog(@"This is _totalInvestment %@ and class %@", _totalInvestment, [_totalInvestment class]);
        NSLog(@"This is _tempCarryover %@ and class %@", _tempCarryover, [_tempCarryover class]);



        NSMutableDictionary* _scenarioProductAttribute = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                          _tempScenarioId, @"scenarioid",
                                                          _resultSelectedWeekValue, @"weekvalue",
                                                          _getProductID, @"productid",
                                                          _tempOzs, @"ounces",
                                                          _getGallons, @"gallons",
                                                          _totalInvestment, @"totalinvestment",
                                                          _tempCarryover, @"carry_n",
                                                          _formatDate, @"date",
                                                          nil];
        [_totalInvestment release];
        _tempScenarioId = [_dbObject insertScenarioProductAttribute:_scenarioProductAttribute andScenarioId:_tempScenarioId      andProductId:_getProductID andSelectedWeekValue:_resultSelectedWeekValue];

Solution

  • change the line to:

    self._resultSelectedWeekValue = [_dbObject getSelectedWeekValue:getSelectedWeek];
    

    if you were facing issues with the "_resultSelectedWeekValue" variable value.