Incorrect decrement of the reference count of an object that is not owned at this point by the caller
这种问题一般就是变量申请了内存并初始化了,但没有使用此变量,接着将此变量又重新赋值。如下:
- NSString *imageString = [[NSString alloc] init];
- imageString = @"HResout";
Problem with stringByAppendingString and retain count
I need to concatenate some strings.
NSString * sportName = [[NSString alloc ]initWithString:@""]; sportName = [sportName stringByAppendingString:[someObject getString]]; // can be more stringByAppendingString ... [sportName release];
But something strange for me give the 'build and analyze' command:
on string:
sportName = [sportName stringByAppendingString:[someObject getString]];
the error:Method returns an Objective-C object with a +0 retain count (non-owning reference)
on string:
[sportName release];
the error: Incorrect decrement of the reference count of an object that is not owned at this point by the caller
I am using it for filling my TableView and it is crashed after loading :(.
NSString * sportName = [[NSString alloc ]initWithString:@""]; sportName = [sportName stringByAppendingString:[someObject getString]];
In 2nd line you are getting a new string which is assigned back to spotName
. So the alloced string in first line is leaked. I don't understand why you need to append in an empty string. Appending to @""
is practically of no effect. And if you have some non-empty string then you can create that as autoreleased string instead of alloc. Like this:
// create an autoreleased string NSString * sportName = @"my_string"; // this will return a new string which is also autoreleased sportName = [sportName stringByAppendingString:[someObject getString]]; // all subsequent call to stringByAppendingString will return autoreleased string //so no need to release spotName
And if you need to retain spotName
then you can retain that or use a property.