gpt4 book ai didi

objective-c - [CFDictionary 发布] : message sent to deallocated instance, 怎么了?

转载 作者:行者123 更新时间:2023-11-28 23:03:21 26 4
gpt4 key购买 nike

我不断收到此错误,表明存在僵尸。我已经将范围缩小到这两种方法,但我终究无法弄清楚问题出在哪里。

有什么想法吗?

-(void) getWeatherDetail: (NSString *)query
{
@try
{
NSString *urlString111 = [NSString stringWithFormat:@"http://www.google.com/ig/api?weather=%@",query];
urlString111 = [urlString111 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString111];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

NSData *returnData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];

if(xmlParser)
{
xmlParser=nil;
[xmlParser release];

}


xmlParser = [[NSXMLParser alloc] initWithData: returnData];
isForeCast = NO;
isFirstForeCastEnd = NO;
if (!self.arrForecastAll) {
self.arrForecastAll = [[NSMutableArray alloc] init];
}
[self.arrForecastAll removeAllObjects];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];

// self.logString = [NSString stringWithFormat:@"%@Date & Time: %@ | Text: Getting Weather detail.\n", self.logString, [NSDate date]];
// [self updateLogFile:self.logString];
}
@catch (NSException *exception) {
NSLog(@"Ad exc %@",[exception description]);
}
@finally {

}
}

还有这个..

-(void)getCurrentCityName
{
//////Method to find current city name
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@try
{
if ([self.arrSettings count] > 0 && [[[self.arrSettings objectAtIndex:3] objectForKey:@"Value"] isEqualToString:@"Current Location"])
{
if (currentlat != 0 && currentlong != 0) {
NSURL *urlString;




NSLog(@"lat %f %f" ,currentlat,currentlong);
urlString =[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps/geo?output=json&oe=utf-8&ll=%f,%f&key=[removed API key]",currentlat,currentlong]];



NSLog(@"\n\n-------------------------TF-Get City Name -Request-----------------------------\n%@\n\n",urlString);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;

[request setURL:urlString];
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"\n\n------------------------TF-Get City Name -Response------------------------\n\n%@\n\n",returnString);


[request release];
NSDictionary *dictAddress=[returnString objectFromJSONString];
NSLog(@"dictAddress %@",[dictAddress description]);

[returnString release];

NSString *strCity = [NSString stringWithFormat:@"Getting Location"];
NSLog(@"strCity %@",strCity);
if ([[dictAddress allKeys] count] > 1)
{

if ([[[[[[dictAddress valueForKey:@"Placemark"] objectAtIndex:0] valueForKey:@"AddressDetails"] valueForKey:@"Country"] valueForKey:@"AdministrativeArea"] valueForKey:@"SubAdministrativeArea"])
{
strCity = [NSString stringWithFormat:@"%@,%@",
[[[[[[[[dictAddress valueForKey:@"Placemark"] objectAtIndex:0] valueForKey:@"AddressDetails"] valueForKey:@"Country"] valueForKey:@"AdministrativeArea"] valueForKey:@"SubAdministrativeArea"] valueForKey:@"Locality"] valueForKey:@"LocalityName"],
[[[[[[dictAddress valueForKey:@"Placemark"] objectAtIndex:0] valueForKey:@"AddressDetails"] valueForKey:@"Country"] valueForKey:@"AdministrativeArea"] valueForKey:@"AdministrativeAreaName"]];
}
else {
strCity = [NSString stringWithFormat:@"%@,%@",
[[[[[[[dictAddress valueForKey:@"Placemark"] objectAtIndex:0] valueForKey:@"AddressDetails"] valueForKey:@"Country"] valueForKey:@"AdministrativeArea"] valueForKey:@"Locality"] valueForKey:@"LocalityName"],
[[[[[[dictAddress valueForKey:@"Placemark"] objectAtIndex:0] valueForKey:@"AddressDetails"] valueForKey:@"Country"] valueForKey:@"AdministrativeArea"] valueForKey:@"AdministrativeAreaName"]];
}

self.strCurrentCity = strCity;
[self getWeatherDetail:self.strCurrentCity];
}

}
else
{
self.strCurrentCity = @"Current Location";
}

}
else
{
self.strCurrentCity = [[self.arrSettings objectAtIndex:3] objectForKey:@"Value"];
[self getWeatherDetail:self.strCurrentCity];
}

// self.logString = [NSString stringWithFormat:@"%@Date & Time: %@ | Text: Getting current city name. CityName: %@\n", self.logString, [NSDate date], self.strCurrentCity];
// [self updateLogFile:self.logString];


[pool release];
}
@catch (NSException *exception) {
NSLog(@"Ad exc %@",[exception description]);
}
@finally {

}
}

最佳答案

这是通过直接 ivar 访问遇到的经典内存管理问题。使用访问器来处理你的 ivars,你的代码会更简单,你不会有这些类型的错误。

所以替换这段代码:

    if(xmlParser)
{
xmlParser=nil;
[xmlParser release];
}

xmlParser = [[NSXMLParser alloc] initWithData: returnData];
isForeCast = NO;
isFirstForeCastEnd = NO;
if (!self.arrForecastAll) {
self.arrForecastAll = [[NSMutableArray alloc] init];
}

与:

    self.xmlParser = [[[NSXMLParser alloc] initWithData: returnData] autorelease];
self.isForeCast = NO;
self.isFirstCoreCastEnd = NO;
if (!self.arrForecastAll) {
self.arrForecastAll = [NSMutableArray array];
}

这也消除了您对 arrForecastAll 的分配中的泄漏。

关于你的第二个 block ,任何时候你释放一个变量,如果变量没有立即离开范围,你应该总是将它设置为nil。因此,例如,您 [request release] 后应跟有 request = nil

顺便说一句,你为什么要把所有这些都放在@try block 中? ObjC 中的异常处理是罕见的、昂贵的并且会泄漏内存。异常通常表示 ObjC 中不可恢复的错误,不是一般的错误处理系统。有时 @try 是合适的,但我在这里看不到任何理由。

关于objective-c - [CFDictionary 发布] : message sent to deallocated instance, 怎么了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9685749/

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