开发者

Releasing 2 times?

I am confused with releasing the object that I first alloc/initialized and then copied. As far as I understood the memory management manual I should release the object 2 times because by allocating it and then later copying it I should have retain value of 2? So first release would lower it to one and second to 0? I get message sent to dealloc object if I release it twice. If I do it once there are no problems in application, but there is one in my understanding of Objetive C mem. management :)))

The only explanation that I can think of is when you release an object from soecific context it will release it instantly no matter the value of retain count ?

In the snippet bellow xmlElement is the confusing one....

// LSnippet of code for TouchXML
for (CXMLElement *resultElement in resultNodes) {

    NSMutableDictionary *xmlElement = [[NSMutableDictionary alloc] init];

    // Create a counter variable as type "int"
    int counter;

    // Loop through the children of the current  node
    for(counter = 0; counter < [resultElement childCount]; counter++) {

        // Add each field to the blogItem Dictionary with the node name as key and node value as the value
        [xmlElement setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
    }

    // Add the blogItem to the global blogEntries Array so that the view can access it.


    [tempReturnedElements addObject:[xmlElement copy]];
    [xmlElement release];    
    //[xmlElement release]; but NOT!


}

UPDATE: whole method code:

+(void) runXPath:(NSString *)xPathExpression{

CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithXMLString:xmlStringContent options:0 error:nil] autorelease];

// Create a new Array object to be used with the looping of the results from the rssParser
NSArray *resultNodes = NULL;


// Set the resultNodes Array to contain an object for every instance of an  node in our RSS feed
resultNodes = [rssParser nodesForXPath:xPathExpression error:nil];

NSMutableArray *tempReturnedElements = [[NSMutableArray alloc]init];

// Loop through the resultNodes to access each items actual data
for (CXMLElement *resultElement in resultNodes) {

    // Create a temporary MutableDictionary to store the items fields in, which will eventually end up in blogEntries
    NSMutableDictionary *xmlElement = [[NSMutableDictionary alloc] init];

    // Create a counter variable as type "int"
    int counter;

    //开发者_开发技巧 Loop through the children of the current  node
    for(counter = 0; counter < [resultElement childCount]; counter++) {

        // Add each field to the blogItem Dictionary with the node name as key and node value as the value
        [xmlElement setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
    }

    // Add the blogItem to the global blogEntries Array so that the view can access it.


    [tempReturnedElements addObject:[xmlElement copy]];
//***** Crushes if I use:
//***** [tempReturnedElements addObject:[[xmlElement copy] autorelease]];
    [xmlElement release];



}


[lotojuegosAppDelegate setMyReturnedXmlElements:[tempReturnedElements copy]];

[tempReturnedElements release];

}

Thanks in advance, Luka


In [xmlElement copy], only the returned object (i.e. the copy) needs to be released. The receiver (xmlElement)'s ownership (retain count) is not changed.

So the correct way should be

    NSDictionary* xmlElemCopy = [xmlElement copy];
    [tempReturnedElements addObject:xmlElemCopy];
    [xmlElemCopy release];
    [xmlElement release];


The rule is pretty simple: for each alloc/init, copy or retain you must call exactly one release or autorelease when you want to give up the ownership. You've only called one alloc/init, so you may only call release once.

This line:

[tempReturnedElements addObject:[xmlElement copy]];

should be written as:

[tempReturnedElements addObject:[[xmlElement copy] autorelease]];

The reason is: copy gives you a new object. The retain count/ownership of the object pointed to by xmlElement is unchanged, but the new object now belongs to you. You're responsible for releasing it now. See the first paragraph in this answer: you called copy, you need to call release on the resulting object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜