저는 Objective C를 처음 접했고 retain 및 release 사용 방법에 대해 아직 명확하지 않습니다. 다음 코드에서 TBXML을 사용하여 XML 파일을 구문 분석하고 TableView를 채 웁니다. 코드는 작동하지만, "분석"할 때 Xcode는 변수 name
에 메모리 누수가 있다고합니다. 그러나 변수를 유지 한 후에 변수를 릴리스해야한다고 가정하지만, 언제든지 변수를 놓으려고 할 때마다 항상 오류가 발생합니다. 나는 또한 그것을 유지하지 않으려 고했지만 또한 오류가 발생했습니다.Objective-C에서 TBXML을 사용할 때 메모리 누수가 발생했습니다.
누군가 여기서 무슨 일이 일어 났는지 설명해 주시겠습니까?
- (void)loadNews {
TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:@"http://www.abc/def.xml"]] retain];
// If TBXML found a root node, process element and iterate all children
if (tbxml.rootXMLElement) {
TBXMLElement *categoryElement = [TBXML childElementNamed:@"category" parentElement:[tbxml rootXMLElement]];
do {
NSString *name = [TBXML valueOfAttributeNamed:@"name" forElement:categoryElement];
[name retain]; // Something wrong with this line?
NewsCategory *newsCategory = [[NewsCategory alloc] initWithCategoryName:name];
// get entries in the category
TBXMLElement *entryElement = [TBXML childElementNamed:@"entry" parentElement: categoryElement];
do {
NSString *title = [TBXML textForElement:[TBXML childElementNamed:@"title" parentElement:entryElement]];
NSString * icon = [TBXML textForElement:[TBXML childElementNamed:@"icon" parentElement:entryElement]];
NSString * link = [TBXML textForElement:[TBXML childElementNamed:@"link" parentElement:entryElement]];
NSString * desc = [TBXML textForElement:[TBXML childElementNamed:@"desc" parentElement:entryElement]];
NewsEntry *newsEntry = [[NewsEntry alloc] init];
newsEntry.title = title;
newsEntry.icon = icon;
newsEntry.link = link;
newsEntry.desc = desc;
[newsCategory addEntry:newsEntry];
[newsEntry release];
} while((entryElement = entryElement->nextSibling));
// save category
[newsData addCategory:newsCategory];
[newsCategory release];
} while((categoryElement = categoryElement->nextSibling));
}
// release resources
[tbxml release];
[newsTableView reloadData];
}
코드가 좋아 보인다. '[newsCategory release]'를 호출하는 곳에서'[name release]'를 호출하면됩니다. 그래서'[name retain]'을 호출하지 않으면 에러가 발생합니다. 어떤 오류입니까? 자동 렌더링 된 객체 여야하므로이 메서드에서 유지/해제하면 안됩니다. 'txxml' 변수도 마찬가지입니다. 자동 리셋되기 때문에 이것을 유지하거나 해제 할 필요가 없습니다. – Sam