Reputation: 462
When i run profile with this code in Instruments, I got 35 memory leaks of Player. How i need to handle to avoid memory leaks?
NSMutableArray *players = [NSMutableArray array];
NSData *data = [theRequest responseData];
NSArray *array = (NSArray *) [decoder objectWithData:data];
int len = 35;
for (int i = 0; i < len; i++) {
NSDictionary *dic = (NSDictionary *) [array objectAtIndex:i];
Player *p = [[Player alloc] init];
p.playerID = [dic objectForKey:@"id"];
p.name = [dic objectForKey:@"name"];
p.country = [dic objectForKey:@"country"];
p.club = [dic objectForKey:@"club"];
p.imageURL = [dic objectForKey:@"image"];
p.likeNumber = [dic objectForKey:@"like_number"];
p.likeTime = [dic objectForKey:@"like_time"];
p.likePlayerID = [dic objectForKey:@"like_player_id"];
p.likeDeviceID = [dic objectForKey:@"like_device_id"];
p.disLikeNumber = [dic objectForKey:@"dislike_number"];
p.disLikeTime = [dic objectForKey:@"dislike_time"];
p.disLikePlayerID = [dic objectForKey:@"dislike_player_id"];
p.disLikeDeviceID = [dic objectForKey:@"dislike_device_id"];
[players insertObject:p atIndex:i];
[p release];
}
Upvotes: 0
Views: 188
Reputation: 3939
Perhaps it thinks your players pointer is being lost at the end of the code block.
Or perhaps it is getting confused over the status of the array.
Does it complain if you create the mutable array with alloc-init instead of array: ? (Using 'array' without a retain does leave the whole thing on the allocation pool.)
Upvotes: 1
Reputation: 49054
The code shown above is correct. The leaks are likely coming from something else that is retaining and not releasing a Player
object.
Note that the Leaks instrument only shows where the leaked object was allocated, not where the incorrect memory management occurred. However, if you click the little gray arrow next to one of the leaked objects, you can actually see the entire retain/release history of the object. You should be able to examine that and figure out where the unbalanced retain came from. You may find the static analyzer (Product -> Analyze
) more useful in tracking down the incorrect memory usage.
Upvotes: 4
Reputation: 1274
Port your project to ARC (Automatic reference counting). You won't need to deal with releasing and retaining anymore.
Upvotes: 0
Reputation: 350
I assume its something in your player class. Make sure you are releasing all of its properties.
Upvotes: 0