Search code examples
iphoneiosmemory-managementgdataxml

Parsing XML using GDataXML returns a NSString object with a retainCount of 95.How to ensure it gets released?


The NSString object that is got by the following code has got a retainCount of 95.

for(GDataXMLElement *ele in [doc.rootElement elementsForName:@"myKey"])
    {
        NSLog(@"myKey %d",[[[ele.children objectAtIndex:0] stringValue] retainCount]);

        [myDict setObject:[[ele.children objectAtIndex:0] stringValue] forKey:@"myKey"];

    }

. so would it get released later when

[myDict removeAllObjects];
[myDict release];

is called.

the problem i am facing is that i have hundreds of strings like this parsed.... and all their retaincounts are around 95...would these strings get released?


Solution

  • the problem i am facing is that i have hundreds of strings like this parsed.... and all their retaincounts are around 95...would these strings get released?

    First, retainCount is useless. Don't call it. No, really, don't use retainCount.

    To answer your question, look to Instruments. Do the objects that you expect to go away actually stay in memory? If so, then turn on reference count tracking and see what still holds references to them (or what retains are unbalanced).

    More likely than not, the XML subsystem is unique-ifying the strings such that only one copy of what may be repeated hundreds of times is in memory. That one copy may be retained dozens or hundreds of times as a result. When you removeAllObjects from myDict, there may still be a reference to the objects. It might even be an autoreleased reference and, thus, will actually go away.

    The only way to know is to look to see if the objects are deallocated via Instruments (or some other means).