我实际上正在我的程序开始解析,这是我发现的parser.m类 在“
”行中的“foundCharacters”方法中,此类中的“泄漏”currentElementValue = [[NSMutableString alloc] initWithString:string];
currentElementValue是NSMutableString,它在parser.h文件中声明
NSMutableString *currentElementValue;
任何人都可以建议我解决这个漏洞的方法,有时会在开始时崩溃我的应用程序......
didStartElement方法: -
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"posts"]) {
appDelegate.newsArray = [[NSMutableArray alloc] init];
}
else
{
if([elementName isEqualToString:@"page"])
{
aNewsInfo = [[NewsInfo alloc] init];
aNewsInfo.page = [[attributeDict objectForKey:@"id"] integerValue];
}
if(![elementName compare:@"smallimage"])
{
currentElementValue = [NSMutableString string];
}
if(![elementName compare:@"largeimage"])
{
currentElementValue = [NSMutableString string];
}
}
NSLog(@"Processing Element :%@",elementName);
}
在这个foundCharacter方法中发现泄漏: - 在行....
currentElementValue = [[NSMutableString alloc] initWithString:string];
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
didEndElement方法: -
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"posts"])
return;
if([elementName isEqualToString:@"page"]) {
[appDelegate.newsArray addObject:aNewsInfo];
NSLog(@"%d",[appDelegate.newsArray count]);
NSLog(@"%@",aNewsInfo.title);
NSLog(@"%@",aNewsInfo.fulltext);
[aNewsInfo release];
aNewsInfo = nil;
}
else {
if ([elementName isEqualToString:@"smallimage"])
{
NSURL *url = [NSURL URLWithString:currentElementValue];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
[aNewsInfo setSmallImageData:image];
}
if(![elementName compare:@"largeimage"])
{
NSURL *imageURL = [NSURL URLWithString:currentElementValue];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
[aNewsInfo setLargeImageData:image];
[image release];
}
[aNewsInfo setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
}
答案 0 :(得分:0)
首先,你需要做一些研究:
转到以下链接:
http://www.cocoadev.com/index.pl?MemoryManagement
http://mattpatenaude.com/ch3-memory-management.html
然后这样做,
currentElementValue = [[[NSMutableString alloc] initWithString:string] autorelease]
并从
中删除[currentElementValue release]
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
(否则会因过度释放而导致崩溃)