- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict{
//currentElenet is NSString
currentElement = [elementName copy];
if ([elementName isEqualToString:@"struct"]) {
self.post = [[Question alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([currentElement isEqualToString:@"string"]) {
post.text = [NSString stringWithFormat:@"%@ %@", post.text, string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"struct"]) {
[conversations addObject:post];
[post release];
post = nil;
}
}
//Question.m file
@synthesize text
-(id)init{
self = [super init];
if (self == nil) {
}
else {
//declared as retain in .h file
text = [[NSString alloc]initWithString:@""];
}
return self;
}
-(void)dealloc{
[super dealloc];
[title release];
}
你们有没有看到任何内存泄漏?我调用NSXML委托方法,它基本上将一个“问题”实例放入NSMutableArray。我检查了仪器,解析时发生了内存泄漏。但我不明白为什么......
答案 0 :(得分:2)
currentElement = [elementName copy];
请阅读文档中的copy
API说明。在那里提到
this method retains the new object before returning it. The invoker of the method, however, is responsible for releasing the returned object.
答案 1 :(得分:1)
您确实需要包含属性声明,以便人们能够确定地回答内存管理问题(因为属性定义了内存的管理方式),但假设所有retain
属性:
currentElement
似乎永远不会被释放
text
似乎永远不会被释放
self.post
被分配了[[Question alloc] init]
的结果。该方法的结果已经是您拥有的对象,post
setter会再次保留它。它应该在方法退出之前释放,顺序如下:
id question = [[Question alloc] init];
self.post = question;
[question release];
(它也应该在dealloc
中发布,或者当你完成它以平衡设定者时)
答案 2 :(得分:0)
currentElement ---没有在解析流程中发布...其余代码看起来正确