首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >发布2次?

发布2次?
EN

Stack Overflow用户
提问于 2011-03-28 16:32:07
回答 2查看 110关注 0票数 0

我对释放我首先分配/初始化然后复制的对象感到困惑。就我对内存管理手册的理解而言,我应该释放对象2次,因为通过分配它,然后再复制它,我应该保留2的值?所以第一个版本会把它降到1,第二个版本会降到0?如果我释放两次,我会收到发送到dealloc对象的消息。如果我这样做一次,在应用上没有问题,但在我对Objetive C mem的理解中有一个问题。管理:))

我能想到的唯一解释是,当你从特定的上下文中释放一个对象时,它会立即释放它,而不管保留计数的值是多少?

在下面的代码片段中,xmlElement是一个令人困惑的……

代码语言:javascript
运行
复制
// 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!


}

更新:完整的方法代码:

代码语言:javascript
运行
复制
+(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];

}

提前谢谢你,卢卡

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-28 16:35:14

规则非常简单:对于每个alloc/initcopyretain,当您想要放弃所有权时,必须只调用一个releaseautorelease。您只调用了一个alloc/init,因此您只能调用release一次。

这一行:

代码语言:javascript
运行
复制
[tempReturnedElements addObject:[xmlElement copy]];

应该写成:

代码语言:javascript
运行
复制
[tempReturnedElements addObject:[[xmlElement copy] autorelease]];

原因是:copy给了你一个新的对象。xmlElement指向的对象的保留计数/所有权保持不变,但新对象现在属于您。你现在要负责发布它。请参阅此答案的第一段:调用copy时,需要在结果对象上调用release。

票数 2
EN

Stack Overflow用户

发布于 2011-03-28 16:34:50

[xmlElement copy]中,只需要释放返回的对象(即副本)。接收方(xmlElement)的所有权(保留计数)不变。

所以正确的方法应该是

代码语言:javascript
运行
复制
    NSDictionary* xmlElemCopy = [xmlElement copy];
    [tempReturnedElements addObject:xmlElemCopy];
    [xmlElemCopy release];
    [xmlElement release];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5456430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档