I have an NSString that I’ve retained, and the only place I release it is in the dealloc method. For some reason, however, later in the program when I try to reference it (its length, specifically), I get a crash, saying [CFString length]:message sent to deallocated instance 0xff32c50.
I explicitly retain the string earlier in the program. Is there any reason why this would be happening? Any help is appreciated.
The string, entityParameter, is declared in the header, and defined later.
Here is some of the code:
entityParameter = [[EntitySearchWindow stringByEvaluatingJavaScriptFromString:@"f();"] retain];
The place where I’m getting the crash looks like this:
if([entityParameter length] != 0 && entityParameter != nil)
{
return;
}
Obviously, it isn’t retained, then.
If by “retained” you mean “assigned to a property”, are you doing:
Or:
Because the former will retain (if the property is declared as
retain) whereas the latter will not. Note that NSString properties should generally be declaredcopy, but that is orthogonal to the question).If your code is as written:
And you really do only
releaseit indealloc, then make sure your containing object hasn’t already been deallocated. That may be happening. Or it might be that you’ve leaked the string reference somewhere and spuriously deleted it without a retain.Using Zombie detection in instruments with “track retain/release events” (or whatever it is called) should show you every last retain/release event on the object, including the one the blew up.