mardi 17 mars 2015

Memory leaks on iOS6


I am writing a method to convert C++ std::map to NSDictionary as below:



NSDictionary* NSDictionaryFromMap(const StringMap& map){
NSMutableDictionary * dict = [NSMutableDictionary dictionary];

NSLog(@"NSDictonaryFromMap() - size:%d", (int)map.size());

for (auto it = map.begin(); it != map.end(); it++){
NSString * key = (0 == it->first.length())?(@""):(@(it->first.c_str()));
NSString * value = (0 == it->second.length())?(@""):(@(it->second.c_str()));

[dict setObject:value forKey:key];
NSLog(@"NSDictonaryFromMap() - key:%@ value:%@", key, value);
}

NSDictionary * nd = [NSDictionary dictionaryWithDictionary:dict];
dict = NULL;

NSLog(@"NSDictonaryFromMap() - dictionary:%@", nd);
return nd;
}


But I got some memory leaks when calling this method. Sorry I cannot post image here. In instrument, it highlights memory leaks on four function call



NSMutableDictionary * dict = [NSMutableDictionary dictionary];
NSString * key = (0 == it->first.length())?(@""):(@(it->first.c_str()));
NSString * value = (0 == it->second.length())?(@""):(@(it->second.c_str()));
[dict setObject:value forKey:key];


On iOS 7 and iOS 8, there is not any memory leaks, but on iOS 6, it had. How to fix those leaks for iOS6 ?


I also posted this issue in Apple Forum: http://ift.tt/1AWFdfR




Aucun commentaire:

Enregistrer un commentaire