开发者

"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 obj开发者_开发百科ectAtIndex: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


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)


That leak probably means that you're over-retaining the return value. And leaks-tool just shows you the place where it was created.


You are leaking the NSArray when you re-assign a new NSArray inside the if block. The pointer to the original array is lost, which means that the runloop cannot release the memory allocated for the first result. The memory allocated for the second result is released.

You should really use a proper parser... however to address the leak, the following will work.

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


What do you do with the object that's returned by this method? [arrRetrive objectAtIndex:0] is likely what's being leaked.

Leaks indicates that line with componentsSeparatedByString: because that's where the strings in the array are allocated. But the array isn't leaked.

Go into Leaks, look at a leaked instance, and click that little arrow button. You'll see all the retains and releases of the leaked object, which should point you to the problem.

"componentsSeparatedByString" Memory leak

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜