我有以下簡單的代碼來跟蹤MKMapView中的MKAnnotations。我通過我想跟蹤的位置列表循環創建註釋。我將註釋添加到我的集合類,一個NSMutableDictionary和NSMutableArray,這些屬性被聲明爲屬性(不是使用ARC,而是使用Xcode 4.2,所以使用「strong」而不是retain,因爲它們被認爲是同義詞,以便與ARC兼容)。iOS:Xcode 4.2:泄漏儀器說我有我的NSMutableArray和NSMutableDictionary泄漏,但我看不到在哪裏
如果我第二次調用這個例程(比如當位置列表被更新時),泄漏工具聲稱註釋泄漏以及集合對象本身。 (我的泄漏列表中有一個NSMUtableDictionary,堆棧跟蹤指向創建字典的行,並將其設置到我的屬性中,我們以及一些與註釋相匹配的小型泄漏,以及有關NSMutableArray和NSMutableDictionary)。但是,我沒有看到任何違反內存管理規則的行爲。我通過alloc/init創建了一個與我的註釋創建配對的發行版。我通過我的屬性「setter」做了隱式保留,當集合被替換時,它們應該被釋放,然後釋放它們的所有內容對象。如果有人能看到我出錯的地方,我會很感激。
@property(strong, nonatomic) NSMutableArray *annotationList;
@property(strong, nonatomic) NSMutableDictionary *annotationLocations;
。
-(void) createMapAnnotations
{
if ([[self mapView] annotations])
[[self mapView] removeAnnotations:[[self mapView] annotations]];
[self setAnnotationList:[NSMutableArray array]];
[self setAnnotationLocations:[NSMutableDictionary dictionary]];
for (JEstablishmentLocation *loc in [[JLocationManager sharedLocationManager] localLocations])
{
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
[annotation setCoordinate:[[loc estCoordinates] coordinate]];
[[self mapView] addAnnotation:annotation];
[[self annotationList] addObject:[NSDictionary dictionaryWithObject:annotation forKey:[loc estKeyValue]]];
[[self annotationLocations] setObject:loc forKey:[annotation description]];
[annotation release];
}
[self centerOnCurrentLocation];
}
你有沒有嘗試清除你的數組和字典之前'失去'他們?乍一看你的代碼似乎很好。 – jv42 2012-01-18 08:34:33
儘管有語義上的等價性,但與保留的交流仍然很強烈,儀器是否仍然顯示泄漏?這不是一個ARC代碼,你確實在dealloc中釋放了兩個ivars,不是嗎? – 2012-01-18 14:11:25
我會嘗試交換強/保留。是的,ivars是在dealloc中釋放的,但是控制器對象並沒有死掉,所以永遠不會被調用。 – chadbag 2012-01-18 16:34:00