J'essaie de comprendre comment ce code provoque une fuite de mémoire, et j'ai du mal à le faire. D'après ce que j'ai lu, ARC ne gère pas les objets CF, et je dois les libérer. J'ai essayé de le faire, mais j'ai toujours des fuites selon l'outil Instruments d'Apple. Tout conseil serait grandement apprécié ... Merci d'avanceFuite de mémoire avec IOPSCopyPowerSourcesInfo()
- (void) getPowerInfo : (id) sender {
CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
CFDictionaryRef pSource = NULL;
const void *psValue;
// if there is no power source
if (CFArrayGetCount(sources) == 0) {
NSLog(@"Number of power sources found: 0; Aborting battery monitoring");
} else { // if there is a power source
// for each power source
for (int i = 0 ; i < CFArrayGetCount(sources) ; i++) {
pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
NSString *currentPowerState = CFDictionaryGetValue (pSource, CFSTR (kIOPSPowerSourceStateKey));
if (!pSource) { // if source is nil
NSLog(@"Power source is nil");
} else if ([currentPowerState isEqualToString:@"AC Power"]) { // if source is adapter
NSLog(@"Mac is plugged in.");
} else { // if source is battery
psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
int curCapacity = 0;
int maxCapacity = 0;
int percent;
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSCurrentCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &curCapacity);
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
CFNumberGetValue((CFNumberRef)psValue, kCFNumberSInt32Type, &maxCapacity);
percent = (int)((double)curCapacity/(double)maxCapacity * 100);
if (!percent) {
NSLog(@"Battery %% is nil");
} else {
NSLog(@"Battery %% is %i", percent);
}
CFRelease(psValue);
}
}
}
CFRelease(sources);
CFRelease(pSource); }