나는 통계 모니터 응용 프로그램을 구축 중이며 XML 파서를 사용하여 데이터를 검색하고 있습니다. obj-c의 메모리 관리 및 retainCount 등에 관한 많은 연구, 학습 및 읽기가 있지만 Instruments에서 메모리 누수가 발생합니다.메모리 관리 누수 문제를 해결하는 방법
for(int counter = 0; counter < [resultElement childCount]; counter++) { //Loop trough the childs
//Instruments Leaks tells me that I have 75% leaks here
[entryItem setObject:[[resultElement childAtIndex:counter] stringValue] forKey:[[resultElement childAtIndex:counter] name]];
}
//Instruments Leaks tells me that I have 25% leaks here
[totalEntries addObject:[entryItem copy]]; //Copy it into the MutableArray
XML 파서에서 메모리 누수가 발생하지만 누출 문제를 해결하는 방법을 모르겠습니다.
- (NSString *)stringValue {
if (_node->type == XML_TEXT_NODE || _node->type == XML_CDATA_SECTION_NODE){
return [NSString stringWithUTF8String:(const char *)_node->content]; //16.7% leaking
}
if (_node->type == XML_ATTRIBUTE_NODE){
return [NSString stringWithUTF8String:(const char *)_node->children->content]; //50.0% leaking
}
NSMutableString *theStringValue = [[[NSMutableString alloc] init] autorelease];
for (CXMLNode *child in [self children]) {
[theStringValue appendString:[child stringValue]]; //33.3% leaking
}
return theStringValue;
}
[totalEntries addObject:[entryItem copy]];
당신이 개체를 복사 라인에서
설명 아주 좋은 분명! –
흠, 이미 addObject 함수에서 entryItem을 릴리스 했으므로 예제에서 실수를 한 것 같습니다. 문제는 내가 게시 한 두 번째 예에서 실제로 발생합니다. – Leon
은 totalEntries가 사용자 지정 개체 또는 컬렉션입니까? – jbat100