我正在制作一个应用程序,可以在 map 上显示用户以及多个餐厅列表。当用户点击图钉时,它会存储注释中的坐标并将它们与用户进行比较以确保它们不同。一旦确定它们不同,它就会将商家的坐标连同用户的坐标一起发送给 Google 以请求指示。代码工作正常,但为了让它这样做,我不得不以导致内存泄漏的方式声明一些变量。我希望清理代码并了解我犯错的地方以及应该处理的正确方法。
下面是我的代码,用于从被点击的注释中获取坐标。如果我尝试初始化 selectedAnnotation 并通过放置 selectedAnnotation = [[MapLocation alloc] init];
在 say viewDidLoad
中分配它的内存,那么它仍然显示为内存泄漏。作为引用,selectedAnnotation
是一个 MapLocation(符合 MKAnnotation)变量,作为一个属性,我拥有它(非原子,保留)和 @synthesize(d)。
我以为只要在内存中分配,只要在viewDidUnload中设置为nil,在dealloc中释放,应该不会有内存问题。我错过了什么?下面是我在 viewDidLoad 中为 selectedAnnotation 分配内存以及下面提供的代码时内存泄漏的屏幕截图。如果我已经分配了内存,并检查变量是否存在,为什么还要为变量重新分配内存?这发生在我点击的任何餐厅 pin 上,但显然不会发生在用户的 pin 上,因为我有在这种情况下发布它的代码。
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
//NSLog(@"Selected annotation view");
// if we don't have the place holder already allocated
// lazy load the MapLocation placeholder variable
if(!selectedAnnotation)
{
selectedAnnotation = [[MapLocation alloc] init];
}
// save the annotation clicked
selectedAnnotation = view.annotation;
// if the annotation selected was is the same as the user's location
if((selectedAnnotation.coordinate.latitude == savedUserLocation.coordinate.latitude) && (selectedAnnotation.coordinate.longitude == savedUserLocation.coordinate.longitude))
{
// set it to nil and release it
selectedAnnotation = nil;
[selectedAnnotation release];
}
}
我在使用以下方法时遇到类似的内存问题。我从 Google 引入了 JSON 数据,以提取用户位置的地址和坐标以显示在 AnnotationView 中。我创建了所有必要的数组和字典来访问信息,但是一旦我为它们分配内存并将它们的值分配给 savedUserLocation,如果我尝试释放 NSDictionary 变量 userLocation,即使作为此方法中的最后一行代码,应用因 [CFDictionary release]: message sent to deallocated instance 0x83ccb60"
而崩溃。我很确定这是因为我正在通过指针设置 savedUserLocation
中的值,一旦内存被释放,该信息就不再存在,那么分配/释放内存的正确方法是什么到哪里我可以访问信息,而不会导致内存泄漏?我也尝试过使用 autorelease
,但同样的问题仍然存在。
这是放置用户 pin 的代码。
- (void)fetchedData:(NSData *)responseData
{
//parse out the json data
NSError *error;
NSDictionary *json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray *results = [json objectForKey:@"results"]; //2
NSUInteger counter = [results count];
NSDictionary *userLocation = [[NSDictionary alloc] init];
//NSString *address = [[NSString alloc] init];
for(NSUInteger i=0; i < counter; i++)
{
userLocation = [results objectAtIndex:i];
// 2) Get the funded amount and loan amount
NSString *address = [[NSString alloc] initWithString:[userLocation objectForKey:@"formatted_address"]];
NSArray *types = [userLocation objectForKey:@"types"];
NSDictionary *geometry = [userLocation objectForKey:@"geometry"];
NSDictionary *location = [geometry objectForKey:@"location"];
float lat = [[location objectForKey:@"lat"] floatValue];
float lon = [[location objectForKey:@"lng"] floatValue];
CLLocationCoordinate2D newCoordinates;
newCoordinates.latitude = lat;
newCoordinates.longitude = lon;
// count how many types there are
NSUInteger numberOfTypes = [types count];
NSString *type = [[NSString alloc] init];
for(NSUInteger j=0; j < numberOfTypes; j++)
{
type = [types objectAtIndex:j];
if([type rangeOfString:@"street_address" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
NSLog(@"%@", address);
if(!savedUserLocation)
{
savedUserLocation = [[MapLocation alloc] init];
}
[savedUserLocation setTitle:@"You are here!"];
[savedUserLocation setSubtitle:address];
[savedUserLocation setCoordinate:newCoordinates];
}
}
}
// determine which location is closest to the user by calling this function
MapLocation *closestLocation = [self determineClosestLocationToUser:allLocations locationOfUser:savedUserLocation];
// send in the user location and the closest store to them to determine appropriate zoom level and
// to center the map between the two
[self determineMapCenterAndZoomLevelFromUser:savedUserLocation andClosestLocation:closestLocation];
if(!pinDropped)
{
// add the annotation to the map and then release it
[mapView addAnnotation:savedUserLocation];
pinDropped = true;
}
}
感谢您提供的所有帮助/建议/建议。我真的很想了解我做错了什么的具体细节,因为我认为我对它有相当不错的把握。
最佳答案
在 didSelectAnnotationView
中,您有以下代码:
selectedAnnotation = nil;
[selectedAnnotation release];
这会导致内存泄漏,因为您将 selectedAnnotation
设置为 nil
并然后对其调用 release
。
对 release
的调用没有任何作用,因为此时 selectedAnnotation
是 nil
而对 nil
的调用会没有什么。这意味着已分配的内存永远不会释放,但由于指针变量已设置为 nil
,当再次调用 didSelectAnnotationView
时,您的代码会分配一个新对象。
您应该调换两个语句的顺序(调用 release
first 和 then 设置为 nil
) .
但是,您不需要分配一个新对象只是为了保留对“选定注释”的引用。
声明一个常规的 ivar(不是保留属性)并将其设置为等于所选注释应该可以工作。
此外, map View 已经有一个名为 selectedAnnotations
的属性,您应该可以使用它(因此您不需要维护自己的 ivar 或属性)。 map View 的属性是一个 NSArray
,但将始终包含 0 个或 1 个对象。在访问索引 0 处的对象之前,请务必检查其计数
。
在 fetchedData
中,您有一些内存泄漏是由不必要的 alloc
调用引起的。
不需要它们,因为在调用 alloc
之后,您将直接为刚刚为其分配内存的指针分配一个新引用。
例如,userLocation
在 for 循环之前被alloc
但随后在循环内您直接将该变量指向 results< 中的对象
数组。
这意味着最初分配给 userLocation
的内存在没有引用的情况下被废弃。当您尝试在 userLocation
上调用 release
时,它会尝试释放一个不是由 fetchedData
中的代码分配的对象。
要至少修复userLocation
,只需声明变量,不要alloc
/init
/release
变量address
和type
(NSString
)也有类似的问题。
关于ios - 关于 MKAnnotation 和 NSString 内存泄漏问题的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11256351/