Skip to content

Instantly share code, notes, and snippets.

Created September 17, 2011 07:21
Show Gist options
  • Select an option

  • Save anonymous/1223722 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/1223722 to your computer and use it in GitHub Desktop.
Memory Leak
-(void) memoryLeak {
NSString *firstString = [[NSString alloc] initWithString:@"ABC"]; // first string retain count is 1.
NSString *secondString = [[NSString alloc] initWithString:@"XYZ"]; // second string retain count is 1.
NSString *str = [firstString retain]; // str retained first string. first string retain count becomes 2.
str = [secondString retain]; // str retained second string. second string retain count becomes 2. However, point to be noted is that str retained first string and was one of the owner of it, but did not release it.
[str release]; // str currently points to second string and releases it. second string retain count is 1.
[secondString release]; // second string releases. retain count is 0.
[firstString release]; // first string releases. retain count is 1. Since str did not release first string, the retain count is still 1, and not 0. Object is still present in the memory, with no references to it. This is called as "Memory Leak".
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment