iPhone 上 NSXMLParser 的内存泄漏
下面是我的代码,Leaks 说我在 NSMutableString alloc 方法周围出现内存泄漏。我确信这是我忽略的事情,如果有人有任何想法请告诉我。谢谢!
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if (!currentValue) {
currentValue = [[NSMutableString alloc] initWithCapacity:[string length]];
}
[currentValue setString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"phone"]){
currentAgent.phone = currentValue;
}
[currentValue release];
currentValue = nil;
}
-Agent是类初始化时创建的自定义对象。 XML 有效并且具有所有适当的开始/结束标记。
below is my code, Leaks says I am getting a memory leak around NSMutableString alloc method. I am sure it is something I simply overlooked, let me know if anyone has any thoughts. Thanks!
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if (!currentValue) {
currentValue = [[NSMutableString alloc] initWithCapacity:[string length]];
}
[currentValue setString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([elementName isEqualToString:@"phone"]){
currentAgent.phone = currentValue;
}
[currentValue release];
currentValue = nil;
}
-Agent is a custom object that was created when the class was initialized. The XML is valid and has all the appropriate begin/end tags.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看这段代码,我认为您的 Agent 类更有可能泄漏电话。假设代理使用
retain
作为电话属性,这将导致电话持续的时间比应有的时间长。即使额外的保留在其他地方,对象的创建者也会因泄漏而“归功于”。
换句话说,在 Agent 中:
Looking over this code, I think it's more likely that your Agent class is leaking phone. Assuming Agent uses
retain
for the phone property, this will cause the phone to persist longer than it should.The creator of the object gets "credited" with the leak, even if the extra retain is somewhere else.
In other words, in Agent: