My iPhone application is Tab based. In a tab, say Security Tab I have three view controllers First, Second, Third. I am passing a string value to Third view controller from Second view controller. selectedAlertDesc is my NSString object of Third view controller.
In Third view controller:
@property (nonatomic,retain) NSString *selectedAlertDesc;
@synthesize selectedAlertDesc = _selectedAlertDesc;
In Second view controller:
Third *controller = [[Third alloc] init];
[controller setSelectedAlertDesc:[[alertArray objectAtIndex:indexPath.row] objectForKey:@"alertDesc"]];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
It works fine, until I change the tab. If I leave the security tab in Third view controller page and come back after accessing some other tab, it crashes. It states, selectedAlertDesc became zombie.
-[CFString stringByReplacingOccurrencesOfString:withString:]: message sent to deallocated instance 0xeb3d760
How can I solve this issue? I believe we should not initialize synthesized objects. Am I forgetting something here?
EDIT:
As suggested I used instruments for checking allocation/retain history. I got the following:- So after the usage of selectedAlertDesc, I retained it. Is it correct way to do?? And it works properly!!
_selectedAlertDesc = [_selectedAlertDesc stringByReplacingOccurrencesOfString:@"opentag" withString:@"<"];
_selectedAlertDesc = [_selectedAlertDesc stringByReplacingOccurrencesOfString:@"closetag" withString:@">"];
[txtTxtVw setText:_selectedAlertDesc];

From the comments:
It turns out that the error is caused by code that directly assigns new values to an instance variable instead of using the
set...method of the related property (or its corresponding dot notation). This bypasses the property attributes and results in assignment of an autoreleased string.(The primary clue is the missing entry in the Instruments retain history.)