一个字符串的位。使用以下代码(示例进行了简化)时,我会不断收到有关停货和潜在泄漏的通知:
int x = 0;
NSString *aString = [NSString alloc]init]; <-- value store to 'aString' during its initialization is never read
if(x == 0)
{
aString = @"This is a string set by x being 0";
} else
{
aString = @"This is a string set by x being something else";
}
aTextLabelOutlet.text = aString;
[aString release];
<-- Potential leak of an object allocated online ... and stored into aString
我不太明白。它被分配一次,然后在使用后释放。它被初始化,然后总是放一些东西,并且总是被读取。
它从不崩溃或实际上不会导致内存泄漏,所以我有点困惑。
最佳答案
您不应该为NSString
分配aString
,因为您在aString
部分中分配给if
。
这应该足够了:
int x = 0;
NSString *aString;
if(x == 0)
{
aString = @"This is a string set by x being 0";
} else
{
aString = @"This is a string set by x being something else";
}
aTextLabelOutlet.text = aString;
关于ios - Xcode中NSString的死存储和潜在泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6998452/