这些天我正在开发一个钓鱼应用程序,我遇到了内存泄漏问题
-(void)requestFinished:(ASIFormDataRequest *) request {
if(hud != nil){
[hud show:NO];
[hud release];
hud = nil;
}
isLoading = NO;
self.responseText = [request responseString];
[self parseXml]; //I am getting leak here
if ( [self.responseText hasPrefix:@"<result>"]) {
UIAlertView *info = [[[UIAlertView alloc] initWithTitle:@" " message:@"Limited Internet access, please find a stronger signal in the area" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]autorelease];
[info show];
}
if (!isRefreshButtonClicked) {
[UIAccelerometer sharedAccelerometer].delegate = self;
[NSThread detachNewThreadSelector:@selector(parseXml) toTarget:self withObject:nil];
} }
这是我的功能......
-(void) parseXml
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
_fishes = [[fishes parseXml:self.responseText] retain];
[self performSelectorOnMainThread:@selector(parseXmlDone) withObject:nil waitUntilDone:YES];
[pool release];
这里_fishes是一个从数组返回类型函数获取值的数组.....这里是该函数...
+(NSMutableArray *)parseXml:(NSString *)xmlString {
//xmlString = [xmlString stringByReplacingOccurrencesOfString:@"&" withString:@""];
const char *cString = [xmlString UTF8String];
NSMutableArray *fishes = [NSMutableArray array];
NSData *xmlData = [NSData dataWithBytes:cString length:strlen(cString)];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:&error];
if (doc == nil) { return nil; }
//parseXml
NSArray *_fishes = [doc.rootElement elementsForName:@"fishery"];
for (GDataXMLElement *_fish in _fishes) {
NSMutableDictionary *fish = [NSMutableDictionary dictionary];
NSArray *ids = [_fish elementsForName:@"id"];
if ([ids count]>0) {
GDataXMLElement *firstId = (GDataXMLElement *)[ids objectAtIndex:0];
[fish setValue:firstId.stringValue forKey:@"id"];
} else continue;
NSArray *names = [_fish elementsForName:@"name"];
if ([names count]>0) {
GDataXMLElement *firstName = (GDataXMLElement *)[names objectAtIndex:0];
[fish setValue:firstName.stringValue forKey:@"name"];...........
........
else continue;
NSArray *distances = [_fish elementsForName:@"distance"];
if ([distances count]>0) {
GDataXMLElement *distance = (GDataXMLElement *)[distances objectAtIndex:0];
[fish setValue:distance.stringValue forKey:@"distance"];
}else continue;
[fishes addObject:fish];
}
[doc release];
return fishes;
}
@end
我希望你们能理解我的问题......感谢
答案 0 :(得分:1)
在-parseXml
,
_fishes = [[fishes parseXml:self.responseText] retain];
如果_fishes
被多次发送,将泄漏任何先前的-parseXml
指向的对象。您可以使用retain
属性而不是实例变量,或者使用释放前一个对象的setter方法,或者在将新(保留)对象分配给_fishes
之前释放上一个对象。