Memory Leaks *Important things*


There’s something hidden in the project that you probably don’t know is there. You’ve likely heard about memory leaks. But what you may not know is that there are actually two kinds of leaks.



  1. The first is the the true memory leak, where an object has not yet been deallocated, but is no longer referenced by anything. Therefore the memory can never be re-used.
  2. The second kind of leak is a bit more tricky. It’s called “unbounded memory growth”. This happens where memory continues to be allocated and is never given a chance to be deallocated.  
If this continues forever, then at some point the system’s memory will be filled and you’ll have a big memory problem on your hands. In iOS this means that the app will be killed by the system


*********************************************************************************


  • If the method name begins with init or copy, the object returned will have a retain count of 1, and no autorelease pending. In other words, you own that object and have to release it when you’re done.
      • NSString * sushiName = [_sushiTypes objectAtIndex:indexPath.row]; // 1
        NSString * sushiString = 
            [[NSString alloc] initWithFormat:@"%d: %@", 
                indexPath.row, sushiName]; // 2
        cell.textLabel.text = sushiString; // 3
        [sushiString release]; // 4
        
        
        
        
  • If the method name begins with anything else, the object returned will have a retain count of 1, and an autorelease pending. In other words, you can use the object right now, but if you want to use it later you have to retain the object.
    • NSString * sushiName = [_sushiTypes objectAtIndex:indexPath.row]; // 1
      NSString * sushiString = 
          [NSString stringWithFormat:@"%d: %@", 
              indexPath.row, sushiName]; // 2
      cell.textLabel.text = sushiString; // 3

*********************************************************************************

Comments

Popular posts from this blog

How to Make REST api call in Objective-C.

Launch iPhone simulator without Xcode.

Hacker Rank problem solution