开发者

instruments showing NSPlaceholderstring leaks

开发者 https://www.devze.com 2023-02-01 14:38 出处:网络
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.

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开发者_JAVA技巧 there are still leaks)

Ideas anyone?


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.


How about in the dealloc method? Did you release the storedHash in the dealloc method? And how about checking 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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消