显示 NSPlaceholderstring 泄漏的仪器

发布于 2024-10-09 18:52:58 字数 712 浏览 0 评论 0原文

我正在尝试减少应用程序中的内存泄漏,因此我使用仪器来查找所有泄漏。我设法消除了几乎所有的泄漏,除了一个非常烦人的泄漏。

Instruments 告诉我,我有很多 NSPlaceholderstring 泄漏。 生成泄漏的代码(根据仪器)是:

if (nil == storedHash) 
{
  NSString *description = [[NSString alloc] initWithFormat:@"1 = %@ 2= %d", uId, service];
  self.storedHash = description; // This line is the leak according to instruments

  [description release];
  description = nil;
}

return storedHash

storedHash 的定义如下:

@property(copy) NSString* storedHash;

我尝试了我能想到的一切:

  • 我使用保留而不是复制
  • 我使用了 NSString(stringWithFormat)的自动释放分配
  • 我尝试用以下代码包装代码自动释放池

以上都没有改变泄漏。 (在某些情况下,泄漏的类型会发生变化,但仍然存在泄漏)

有人有想法吗?

I'm trying to reduce the memory leaks in my app, so i used instruments to find all the leaks. I managed to remove almost all of the leaks, except a very annoying one.

Instruments is telling me that i have a lot of NSPlaceholderstring leaks.
The code that generated the leak (according to instruments) is:

if (nil == storedHash) 
{
  NSString *description = [[NSString alloc] initWithFormat:@"1 = %@ 2= %d", uId, service];
  self.storedHash = description; // This line is the leak according to instruments

  [description release];
  description = nil;
}

return storedHash

storedHash is define like this:

@property(copy) NSString* storedHash;

I tried everything i can think of:

  • I used retain instead of copy
  • I used an autorelease allocation of the NSString (stringWithFormat)
  • I tried wrapping the code with an autorelease pool

Nothing of the above changed the leak. (In some cases the type of the leaks change, but there are still leaks)

Ideas anyone?

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

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

发布评论

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

评论(3

无畏 2024-10-16 18:52:58

在哪里释放storedHash?你在dealloc中释放它吗?

请注意,NSPlaceholdeString 是一个实现细节;它是 NSString+alloc 方法返回的实例的类。

Where do you release storedHash? Do you release it in dealloc?

Note that NSPlaceholdeString is an implementation detail; it is the class of the instance returned by NSString's +alloc method.

孤者何惧 2024-10-16 18:52:58

dealloc 方法中怎么样?你在dealloc方法中释放了storedHash吗?那么如何检查 if (nil == self.storedHash)

How about in the dealloc method? Did you release the storedHash in the dealloc method? And how about checking if (nil == self.storedHash)

相思故 2024-10-16 18:52:58

您应该使用

@property(nonatomic, retain) NSString* storedHash;

复制来代替。 @property(copy) 没有释放你的旧 NSObject,你应该自己做:

if (nil == storedHash) 
{
  NSString *description = [[NSString alloc] initWithFormat:@"1 = %@ 2= %d", uId, service];
  [self.storedHash release];
  self.storedHash = description; // This line is the leak according to instruments

  [description release];
  // description = nil; // it's unnecessary
}

你也应该在 dealloc 中释放storedHash。

You should use

@property(nonatomic, retain) NSString* storedHash;

instead copy. @property(copy) didn't release your old NSObject and you should do it yourself:

if (nil == storedHash) 
{
  NSString *description = [[NSString alloc] initWithFormat:@"1 = %@ 2= %d", uId, service];
  [self.storedHash release];
  self.storedHash = description; // This line is the leak according to instruments

  [description release];
  // description = nil; // it's unnecessary
}

also you should release storedHash in dealloc.

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