My iPhone app keeps receiving BAD_EXC_ACCESS error after running for a while, after painful debugging I find that one of my static variables (NSPredicate to be exact) was corrupted: it still pointed to the original address, but its isa variable was reset to 0!
This totally beats me, how come the isa variable was reset to 0 ?! Any idea ?
Thanks!
Update: post the code. The code is actually from erica's ABContactHelper I changed it a little bit to use Predicate Templates to make query more efficient, so I added this function:
+ (NSPredicate*) predicateforMatchingName:(NSString*) name {
//templateForMatchingName is a static variable I declare elsewhere
//static NSPredicate * templateForMatchingName =nil;
if (templateForMatchingName == nil) {
templateForMatchingName = [NSPredicate predicateWithFormat:@"firstname contains[cd] $NAME1 OR lastname contains[cd] $NAME2 OR nickname contains[cd] $NAME3 OR middlename contains[cd] $NAME4"];
}
NSDictionary *sub = [NSDictionary dictionaryWithObjectsAndKeys:
name,@"NAME1",
name,@"NAME2",
name,@"NAME3",
name,@"NAME4",
nil];
NSPredicate *pred = [templateForMatchingName predicateWithSubstitutionVariables:sub];
return pred;
}
I have thought this code is perfect "normal", templateForMatchingName is impossible to be changed after it was created. But then I find its isa variable was reset. It is not necessarily set to nil. This time I find it was reset to different value. And the weird thing is that it still pointed to the original area.
Any idea?
You are calling a class method that returns an autoreleased object; try adding a nested retain during instantiation, or just use alloc&init.
And yes, the pointer would point to the original address, that's why it throws that exception. If the pointer was set to nil it would have instantiated your object again due to the if statement.