Xcode 中 NSString 的死存储和潜在泄漏

发布于 2024-11-28 19:59:05 字数 572 浏览 1 评论 0原文

有点像一根绳子。使用以下代码(针对示例进行简化)时,我不断收到死存储和潜在泄漏的通知:

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

我不太明白这一点。它被分配一次,然后在使用后释放。它被初始化,然后总是有一些东西被放入其中并且总是被读取。

它从未崩溃或实际上导致内存泄漏,所以我有点困惑。

Bit of a string one. I keep getting notified of a Dead Store and Potential Leak when using the following code (simplified for the example):

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

I don't really understand this. It is alloc'ed once then released after use. It is initialised then always has something put into it and is always read.

It never crashed or actually causes a memory leak so I'm a bit confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情栀口红 2024-12-05 19:59:06

您不应该为 aString 分配 NSString,因为您在 if 部分分配给 aString

这应该足够了:

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;

You shouldn't allocate NSString for aString, since you assign to aString in the if part.

This should be enough:

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;
泼猴你往哪里跑 2024-12-05 19:59:06

问题是你已经分配了一个 NSString 但你没有释放它。您正在尝试释放使用 aString = @"..." 代码分配的字符串。

由于对 aString 的第一次赋值从未被使用过,那么您不需要为 aString 提供另一个答案中提到的值(这也不会执行释放,因为字符串是静态分配的常量)

如果 String 确实有使用过的值您应该交换它们并释放它们。例如

int x = 0;
NSString *aString = [NSString alloc]init]; <-- value store to 'aString' during its     initialization is never read

// do something with aString
[aString release];

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;

,或者我会使用自动释放的字符串,这样运行时就会执行释放,

NSString *aString = [[[NSString alloc]init]autorelease];

或者更好地使用构造函数类方法之一

NSString *aString = [NSString string];

The issue is that you have allocated a NSString but then you do not release it. You are trying to release the string you assigned with the aString = @"..." code.

As the first assignment to aString is never used then you don't need to give aString a value as noted in the other answer (which also does not do a release as the strings are stsically allocated constants)

If a String did have a used value them you should swap over the assignment and release. e.g.

int x = 0;
NSString *aString = [NSString alloc]init]; <-- value store to 'aString' during its     initialization is never read

// do something with aString
[aString release];

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;

Or I woulduse an autoreleased string so hat the runtime will do the releasing i.e.

NSString *aString = [[[NSString alloc]init]autorelease];

or better use one of the constructer class methods

NSString *aString = [NSString string];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文