NSString * path = [[NSBundle mainBundle] pathForResource: @ "sortednames" ofType: @ "plist"];
NSDictionary * dict = [[NSDictionary alloc] initWithContentsOfFile: path];
self.names = dict;
[Dict release];
NSArray * arr = [[names allKeys] sortedArrayUsingSelector: @ selector (compare :)];
self.keys = arr;
Do not write that right:
NSString * path = [[NSBundle mainBundle] pathForResource: @ "sortednames" ofType: @ "plist"];
self.names = [[NSDictionary alloc] initWithContentsOfFile: path];
self.keys = [[names allKeys] sortedArrayUsingSelector: @ selector (compare :)];
Reply:
http://blog.csdn.net/NickTang written as this will cause a memory leak, self.names = [[NSDictionary alloc] initWithContentsOfFile: path]; such a sentence, the first execution = back part, apply a piece of memory, and the memory counter is initialized to 1, and then perform the operation on the left = equal sign operator is self.names, is to call names of setter functions, and names at the time of declaration, retain the use of the modifier, then be in its setter function to do a new memory address retain, thus causing this memory memory counter is 2, the rest of the You consider yourself.
If you need further information, please visit my blog. http://blog.csdn.net/NickTang
Reply:
No, each assignment more than once allocated memory.
self.names = [[NSDictionary alloc] initWithContentsOfFile: path];
The right [[NSDictionary alloc] initWithContentsOfFile: path]; make retainCount +1, this should know that no problem.
In the setter inside the left self.names attributes can have a retainCount +1 (attribute setter method names run by the compiler when building)
Its true achieve the following:
- (Void) setnames: (NSString *) names
{
[_names Release];
_names = [names retain]; / / Here the addition of a retainCount +1
}
Then the retainCount of 2
And we are finished using the properties of the attribute release again, this time only the reference count becomes 2-1 = 1. As long as the reference count is not 0, it will never be part of the memory recovery system, likely to cause a memory leak.
Reply:
To self.names landlord break it.
self.names = [[NSDictionary alloc] initWithContentsOfFile: path];
This wording will cause a memory leak, but if behind the expression is autorelease not be a problem.
Reply:
2nd Floor
No comments:
Post a Comment