4
votes

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?

3

3 Answers

5
votes

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.

0
votes

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

0
votes

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.