Tools showing NSPlaceholderstring cord leaks

I am trying to reduce memory leaks in my application, so I used tools to find all the leaks. I managed to remove almost all the leaks, except for the very annoying one.

The tools tell me that I have a lot of NSPlaceholderstring leaks. The code that created the leak (according to the tools):

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 defined as follows:

 @property(copy) NSString* storedHash; 

I tried everything I could think:

  • I used save instead of copy
  • I used automatic selection NSString (stringWithFormat)
  • I tried wrapping the code with the autoplay pool

None of the above has changed the leak. (In some cases, the type of leaks varies, but there are still leaks)

Anyone ideas?

+4
source share
3 answers

Where do you release storedHash ? Will you let him go at dealloc ?

Note that NSPlaceholdeString is an implementation detail; this is the instance class returned by the NSString +alloc .

+5
source

What about the dealloc method? Did you release the stored hash in the dealloc method? What about checking if (nil == self.storedHash)

0
source

You have to use

 @property(nonatomic, retain) NSString* storedHash; 

copy instead. @property(copy) did not release your old NSObject, and you have to 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 unnecessary } 

also you have to release storedHash in dealloc.

0
source

Source: https://habr.com/ru/post/1333482/


All Articles