Ceci est un cas courant:
Dans le code ci-dessous (simplifié), le nombre de dictionnaires ajoutés au tableau varie en fonction de la taille de l'ensemble de données (comme d'habitude).
Il est donc impossible de faire correspondre chaque dictionnaire avec une instruction 'release' correspondante.
Je suppose que libérer le tableau (dans dealloc) libérerait tous ses dictionnaires inclus? Néanmoins, cela produit une fuite. Comment l'éliminer?Comment éviter les fuites sur les tableaux avec les dictionnaires utilisés par CorePlot
// -----myController.h----
@interface ExerciseGraphController : UIViewController <CPPlotDataSource>{
NSMutableArray *plotDataForBar;
}
@property(readwrite, retain, nonatomic)NSMutableArray *plotDataForBar;
// -----myController.m----
- ...getPlotData... {
//Solution
if (plotDataForBar){
[plotDataForBar release];
}
plotDataForBar = [[NSMutableArray array] init];
//for each item in plotDataForBar {
... //Populates plotDataForBar with dictionaries.
[plotDataForBar addObject: [NSDictionary dictionaryWithObjectsAndKeys: // **LEAK** (on __NSCFDictionary, __NSArrayM, and _GeneralBlock16)
[NSDecimalNumber numberWithFloat:xF], [NSNumber numberWithInt:CPBarPlotFieldBarLocation],
[NSDecimalNumber numberWithFloat:yF], [NSNumber numberWithInt:CPBarPlotFieldBarLength],
nil]];
}
} //Reads plotDataForBar in:
- ...numberOfRecordsForPlot...{ // Method required by CorePlot
return [plotDataForBar count];
}
- ...numberForPlot... { // " " " "
...
NSDecimalNumber *num = [[plotDataForBar objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
...
}
- ...dealloc {
[plotDataForBar release];
[super dealloc];
}
C'était tout! J'ai ajouté 'Solution' au code ci-dessus. Merci beaucoup! – sambaMan