开发者

Memory leaks in NSMutableDictionary

开发者 https://www.devze.com 2023-01-09 18:52 出处:网络
When i run the instrument tool i\'m getting memory leaks in the following lines,\"(NSString *)feedString\" is JSON response i\'m getting from the server,can any one give and idea how can i fix this me

When i run the instrument tool i'm getting memory leaks in the following lines,"(NSString *)feedString" is JSON response i'm getting from the server,can any one give and idea how can i fix this memory leak,

Thanks, Sam,

 NSMutableDictionary *allCards = [NSMutableDictionary dictionary];

CardTypeDTO *cardTypeDTO = [[CardTypeDTO alloc] init];

  [cardTypeDTO setImageURL:[[CommonUtility urlDecode:[cardDetail objectForKey:@"imageURL"]] retain]];

  [allCards setValue:cardTypeDTO forKey:[cardDetail objectForKey:@"accountId"]];



-(id)parseFeed:(NSString *)feedString{

 //NSMutableDictionary *allCards = [[NSMutableDictionary alloc] init];
 NSMutableDictionary *allCards = [NSMutabl开发者_JAVA百科eDictionary dictionary];


 NSMutableDictionary *parsedOutput = (NSMutableDictionary *)feedString;

 NSArray *objectKeys = [parsedOutput allKeys];


 for (int i = 0; i < [objectKeys count]; i++) {
  NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
  NSMutableDictionary *cardDetail = [parsedOutput objectForKey:[objectKeys objectAtIndex:i]];
  CardTypeDTO *cardTypeDTO = [[CardTypeDTO alloc] init];
  [cardTypeDTO setAccountId:[cardDetail objectForKey:@"accountId"]];
  [cardTypeDTO setCardName:[cardDetail objectForKey:@"cardName"]];
  [cardTypeDTO setBankName:[cardDetail objectForKey:@"bankName"]];
  [cardTypeDTO setCustomMessage:[cardDetail objectForKey:@"customMessage"]];
  [cardTypeDTO setAvailableAmount:[cardDetail objectForKey:@"availableAmount"]];
  [cardTypeDTO setBalanceAmount:[cardDetail objectForKey:@"balanceAmount"]];
  [cardTypeDTO setPercentage:[cardDetail objectForKey:@"percentage"]];
  //[cardTypeDTO setImageURL:[cardDetail objectForKey:@"imageURL"]];
  [cardTypeDTO setImageURL:[[CommonUtility urlDecode:[cardDetail objectForKey:@"imageURL"]] retain]];
  [cardTypeDTO setNickName:[cardDetail objectForKey:@"nickName"]];
  [cardTypeDTO setBalanceStatusCode:[cardDetail objectForKey:@"balanceStatusCode"]];
  [cardTypeDTO setBalanceStatusMsg:[cardDetail objectForKey:@"balanceStatusMsg"]];
  [cardTypeDTO setOnlineLinked:[cardDetail objectForKey:@"onlineLinked"]];
  [cardTypeDTO setCardType:[cardDetail objectForKey:@"cardType"]];

  [allCards setValue:cardTypeDTO forKey:[cardDetail objectForKey:@"accountId"]];
  //[cardTypeDTO release];
  [loopPool release];

 }

 return allCards;
}

+(NSString *) urlDecode: (NSString *) url
{
 NSString *result=[url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 return [result autorelease];
}


I'm surprised it works at all. You cast an NSString to an NSMutableDictionary which seem a bit odd. If it is correct, the leak might just be a false positive due to the unusual code.

I don't see a leak in feedString but I do see one in cardTypeDTO. You've commented out the release but this is not correct.


You really need to review iPhone memory rules. For example

+(NSString *) urlDecode: (NSString *) url
{
   NSString *result=[url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   return [result autorelease];
}

The result of stringByReplacingPercentEscapesUsingEncoding is already autoreleased and you are autoreleasing it again.

On the return, you do this:

[cardTypeDTO setImageURL:[[CommonUtility urlDecode:[cardDetail objectForKey:@"imageURL"]] retain]]

is setImageUrl a synthesized retain @property? If so, it already calls retain.

cardTypeDTO has retainCount 1 and then is added to a dictionary that retains it (setValue calls setObject, which sends retain).

I wrote this, which might help:

http://loufranco.com/blog/files/managing-memory-iphone.html

There is also a link there to another good explanation.

0

精彩评论

暂无评论...
验证码 换一张
取 消