gpt4 book ai didi

objective-c - XML 解析类在重新运行时泄漏字符串

转载 作者:行者123 更新时间:2023-11-28 17:43:52 25 4
gpt4 key购买 nike

我已经处理这个问题两天了,无论我做什么,我都无法让它停止泄漏字符串。

该类是一个 XML 解析器(使用 TouchXML),旨在在应用程序的整个生命周期内重复运行。第一次运行时,没有泄漏,一切都完美清理。在第二次运行时,它开始泄漏,几乎总是在字符串所在的位置。

Instruments 中的一些图片:

http://www.producerstudio.net/1.png

http://www.producerstudio.net/2.png

.h

#import <Foundation/Foundation.h>
#import "TouchXML.h"

@protocol RSSParsingComplete
-(void)parsingFinished;
@end

@interface RSS : NSObject<NSXMLParserDelegate>{
NSArray *rssURLArray;
NSMutableData *xmlData;
NSMutableArray *articles;
NSMutableArray *arrayOfArticles;






int numberOfFeeds;
NSDateFormatter *inputFormatter;
NSDateFormatter *outputFormatter;

id<RSSParsingComplete> delegate;
}

@property (nonatomic, retain) NSArray *rssURLArray;
@property (nonatomic, retain) NSMutableData *xmlData;
@property (nonatomic, retain) id<RSSParsingComplete> delegate;

@property (nonatomic, retain) NSMutableArray *articles;
@property (nonatomic, retain) NSMutableArray *arrayOfArticles;

@property (nonatomic, retain) NSDateFormatter *inputFormatter;
@property (nonatomic, retain) NSDateFormatter *outputFormatter;

-(id)initWithRSSArray:(NSArray *)inputURLArray;
-(void)connect;
-(NSArray *)feedArticles;

@end

.m

#import "RSS.h"

@implementation RSS
@synthesize xmlData, rssURLArray, articles, arrayOfArticles, delegate, inputFormatter, outputFormatter;

-(void)connect{
self.xmlData = [[[NSMutableData alloc] init] autorelease];
NSURL *rssURL = [[NSURL alloc] initWithString:[self.rssURLArray objectAtIndex:numberOfFeeds-1]];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:rssURL] delegate:self];
[urlConnection release];
[rssURL release];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[self.xmlData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.xmlData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[xmlData release];
[connection release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
CXMLDocument *xmlDoc = [[[CXMLDocument alloc] initWithData:xmlData options:0 error:nil] autorelease];
self.articles = [[[NSMutableArray alloc] init] autorelease];

self.inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
self.outputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[self.inputFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
[self.inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[self.inputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[self.outputFormatter setDateFormat:@"dd.MM.yyyy HH:mm:ss"];
[self.outputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[self.outputFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];

NSArray *itemNodes = [xmlDoc nodesForXPath:@"//item" error:nil];
for(CXMLElement *node in itemNodes){
NSMutableDictionary *article = [[NSMutableDictionary alloc] init];
for(int counter = 0; counter < [node childCount]; counter++){
if([[[node childAtIndex:counter] name] isEqualToString:@"title"]){
[article setObject:[[node childAtIndex:counter] stringValue] forKey:@"title"];
}
if([[[node childAtIndex:counter] name] isEqualToString:@"link"]){
[article setObject:[[node childAtIndex:counter] stringValue] forKey:@"url"];
}
if([[[node childAtIndex:counter] name] isEqualToString:@"description"]){
[article setObject:[[node childAtIndex:counter] stringValue] forKey:@"description"];
}
if([[[node childAtIndex:counter] name] isEqualToString:@"pubDate"]){
NSDate *tempDate = [self.inputFormatter dateFromString:[[node childAtIndex:counter] stringValue]];
[article setObject:[self.outputFormatter stringFromDate:tempDate] forKey:@"name"];
}
}
[self.articles addObject:article];
[article release];
}

NSArray *feedTitleNode = [xmlDoc nodesForXPath:@"//title" error:nil];
NSString *feedTitle = [[NSString alloc] initWithString:[[[feedTitleNode objectAtIndex:0] childAtIndex:0] stringValue]];
[self.articles addObject:feedTitle];
[feedTitle release];

[self.arrayOfArticles addObject:[articles copy]];
[self.articles removeAllObjects];
[inputFormatter release];
[outputFormatter release];
numberOfFeeds--;
if(numberOfFeeds > 0){
[self connect];
}else{
[delegate parsingFinished];
}
}



-(NSArray *)feedArticles{
NSLog(@"Array of Articles: %@", self.arrayOfArticles);
return self.arrayOfArticles;
}

-(id)initWithRSSArray:(NSArray *)inputURLArray{
self = [super init];
if (self) {
self.arrayOfArticles = [[[NSMutableArray alloc] init] autorelease];
self.rssURLArray = [[[NSArray alloc] initWithArray:inputURLArray] autorelease];
numberOfFeeds = [self.rssURLArray count];
[self connect];
}
return self;
}

-(void)dealloc{
[rssURLArray release];
[xmlData release];
[articles release];
[arrayOfArticles release];
[super dealloc];
}

- (id)init
{
self = [super init];
return self;
}

@end

我已尽我所能解决泄漏问题。我已经阅读了 Apple 内存管理指南,以及关于 iPhoneDevSDK 的优秀指南,这帮助我减少了 90% 的泄漏(只要调用一次,该类就不会泄漏)。也许我已经盯着这个看太久了,或者我错过了一些明显的东西。

我很感激!

最佳答案

首先,是否应该保留delegate?我不确定你为什么需要它作为实例变量。但是由于它被保留了(而且您似乎没有释放它),您的 RSS 对象将保留对其自身的循环引用并且永远不会被释放。

其次,您是否需要将日期格式化程序保存在一个实例变量中?看起来您正在以相同的方法分配和释放它们。请注意,它们保留在 RSS 实例中并且永远不会释放。

关于objective-c - XML 解析类在重新运行时泄漏字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7005410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com