我对释放我首先分配/初始化然后复制的对象感到困惑。就我对内存管理手册的理解而言,我应该释放对象2次,因为通过分配它,然后再复制它,我应该保留2的值?所以第一个版本会把它降到1,第二个版本会降到0?如果我释放两次,我会收到发送到dealloc对象的消息。如果我这样做一次,在应用上没有问题,但在我对Objetive C mem的理解中有一个问题。管理:))
我能想到的唯一解释是,当你从特定的上下文中释放一个对象时,它会立即释放它,而不管保留计数的值是多少?
在下面的代码片段中,xmlElement是一个令人困惑的……
// 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!
}
更新:完整的方法代码:
+(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];
}
提前谢谢你,卢卡
发布于 2011-03-28 16:35:14
规则非常简单:对于每个alloc/init
、copy
或retain
,当您想要放弃所有权时,必须只调用一个release
或autorelease
。您只调用了一个alloc/init
,因此您只能调用release
一次。
这一行:
[tempReturnedElements addObject:[xmlElement copy]];
应该写成:
[tempReturnedElements addObject:[[xmlElement copy] autorelease]];
原因是:copy
给了你一个新的对象。xmlElement
指向的对象的保留计数/所有权保持不变,但新对象现在属于您。你现在要负责发布它。请参阅此答案的第一段:调用copy时,需要在结果对象上调用release。
发布于 2011-03-28 16:34:50
在[xmlElement copy]
中,只需要释放返回的对象(即副本)。接收方(xmlElement
)的所有权(保留计数)不变。
所以正确的方法应该是
NSDictionary* xmlElemCopy = [xmlElement copy];
[tempReturnedElements addObject:xmlElemCopy];
[xmlElemCopy release];
[xmlElement release];
https://stackoverflow.com/questions/5456430
复制相似问题