简体   繁体   English

显示NSPlaceholderstring泄漏的工具

[英]instruments showing NSPlaceholderstring leaks

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. 仪器告诉我,我有很多NSPlaceholderstring泄漏。 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: storedHash定义如下:

@property(copy) NSString* storedHash;

I tried everything i can think of: 我尝试了我能想到的一切:

  • I used retain instead of copy 我使用retain而不是copy
  • I used an autorelease allocation of the NSString (stringWithFormat) 我使用了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? 想法有人吗?

Where do you release storedHash ? 你在哪里发布storedHash Do you release it in dealloc ? 你是用dealloc发布的吗?

Note that NSPlaceholdeString is an implementation detail; 请注意, NSPlaceholdeString是一个实现细节; it is the class of the instance returned by NSString 's +alloc method. 它是NSString+alloc方法返回的实例的类。

How about in the dealloc method? dealloc方法怎么样? Did you release the storedHash in the dealloc method? 你是否在dealloc方法中释放了storedHash? And how about checking if (nil == self.storedHash) 如何检查if (nil == self.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: @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
}

also you should release storedHash in dealloc. 你也应该在dealloc中释放storedHash。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM