Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

objective c - "componentsSeparatedByString" Memory leak

I am facing some strange memory leak in our existing iPad application, Here is a function which gives memory leak in instrument

-(NSString *)retriveInfo:(NSString*)fromstring:(NSString*)searchstring
{
    NSArray *arrRetrive = [fromstring componentsSeparatedByString:searchstring];
    if([arrRetrive count]!=0){
       if ([arrRetrive count]!=1){
            NSString *strDisplayOrder = [arrRetrive objectAtIndex:1];
            arrRetrive = [strDisplayOrder componentsSeparatedByString:@"<"];   //MEMORY LEAK
       }
     }

     return [arrRetrive objectAtIndex:0];
}

Here is a input parameter

Param 1 : <displayorder>1</displayorder><filename>201103153_0100.pdf</filename><title>【1面】東日本巨大地震直撃、日経平均一時675円安[br]原発関連売られる、東電はS安</title><category>トップ?注目株</category><dirpath>/var/www/mssite/webapp/data/pdf/20110315</dirpath><TimeStamp>201103141700</TimeStamp><FirstPageImg>20110315top.png</FirstPageImg></pagedata>

Param 2: <displayorder>

Basically i want to found (parse) value between start and end tag. (I know NSXMLParser class but asper i explain this one is existing code and if i changed in code its too much time consuming)

Any suggetion?

With Regards Pankaj Gadhiya

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code you've posted doesn't look like it's leaking memory -- all the methods you're calling are of the autorelease type (i.e. there's no new, alloc, copy, or retain in the code).

It's probably the code you have that calls retrieveInfo and does something with the result that's leaking memory (e.g. overretaining it). The leaks tool is pointing you at the componentsSeparatedByString because that's where the memory was allocated which was eventually involved in a memory leak.

Can you show us how you call retrieveInfo and what you do with the result?

Btw, what's the point of this nested if?

if([arrRetrive count]!=0){
   if ([arrRetrive count]!=1)

It's wasteful, you might as well write this and get the same effect:

if ([arrRetrive count] > 1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...