Ok, I spent the last 8 hours fighting with it – it just seems beyond me. Here’s my complete (relevant) code:
- (void)updateUserDefaults
{
NSMutableDictionary *viewControllerDetails = [[NSMutableDictionary alloc] initWithCapacity:4];
[viewControllerDetails setObject:[NSNumber numberWithInt:OOVenueClassControllerType] forKey:@"classType"];
[viewControllerDetails setObject:self.searchTerm forKey:@"searchTerm"];
[viewControllerDetails setObject:self.searchLocation forKey:@"searchLocation"];
//----- the next two lines cause the problem
NSString *res = [[NSString stringWithFormat:@"%@",[searchResults xmlString]] retain];
[viewControllerDetails setObject:res forKey:@"searchresults"];
//-----
NSMutableArray *viewControllersList = [NSMutableArray array] ;
[viewControllersList addObject:viewControllerDetails];
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
//the following line causes the error
[defs setObject:viewControllersList forKey:kViewControllersKey];
[defs synchronize];
[res release];
}
Note the block with the next two lines cause the problem. At first I didn’t create another string, but added it later while trying to solve the problem.
If I comment out those two lines, everything works fine. If I put them back in, I get
- [CFString class]: message sent to deallocated instance 0xa1a9000
Is something is wrong with the string that I’m trying to put into the userdefaults? That string is rather large (about 200,000 characters), but I had stored even longer strings in user defaults in the past.
It’s also worth noting that if I uninstall the app, then everything works fine. But on subsequent runs the problem exhibits itself.
So, how and why and where is the string getting deallocated? I have explicitly added retain – but that still doesn’t help. What am I missing?
Edit: just realised I forgot to say that the error is thrown on line
[defs setObject:viewControllersList forKey:kViewControllersKey];
Also, for general information, method - (NSString *)xmlString on searchResults does exactly what the name means: creates an XML string with the information from that object.
Edit 2: I tried doing something else with that string – convert it to NSData, compress using zlib – but regardless of data type, that particular object gets deallocated. Does it have to do something with the size of the string?
Ok, not sure what exactly the problem was, but it was somewhere in the
searchResultsand/orxmlStringmethod.searchResultsobject is originally created from XML received from the server (XML is parsed into the object structure). WhenxmlStringwas called, for some reason the string I was getting back was different from the original XML (I’m not talking about formatting, of course) – of 200,000 char-long string, within the first 500 chars or so there were some differences. I haven’t been able to figure out why. So, instead of recreating the xml from object structure, I instead stored the original XML in a field in that object and, whenxmlStringwas called, simply returned the original string. Now everything worked fine.Thank you all for your support through this painful process.