Skip to content

Commit

Permalink
fix to close bufferedReaders in CpuFreqDataModule
Browse files Browse the repository at this point in the history
  • Loading branch information
Manabu-GT committed Sep 2, 2017
1 parent cf1d944 commit ffe516a
Showing 1 changed file with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ public void run() {
for (File cpuFile : cpuFiles) {
double minFreq = -1f;
double maxFreq = -1f;
BufferedReader minFreqReader = null;
BufferedReader maxFreqReader = null;
try {
BufferedReader minFreqReader =
new BufferedReader(new FileReader(cpuFile.getAbsolutePath() + "/cpufreq/cpuinfo_min_freq"));
BufferedReader maxFreqReader =
new BufferedReader(new FileReader(cpuFile.getAbsolutePath() + "/cpufreq/cpuinfo_max_freq"));
minFreqReader = new BufferedReader(new FileReader(cpuFile.getAbsolutePath() + "/cpufreq/cpuinfo_min_freq"));
maxFreqReader = new BufferedReader(new FileReader(cpuFile.getAbsolutePath() + "/cpufreq/cpuinfo_max_freq"));
minFreq = parseDouble(minFreqReader.readLine());
maxFreq = parseDouble(maxFreqReader.readLine());

Expand All @@ -70,6 +70,17 @@ public void run() {
}
} catch (IOException ie) {
Log.w(TAG, "Error reading the min/max cpufreq", ie);
} finally {
if (minFreqReader != null) {
try {
minFreqReader.close();
} catch (IOException ignore) {}
}
if (maxFreqReader != null) {
try {
maxFreqReader.close();
} catch (IOException ignore) {}
}
}
cachedFrequencies.put(cpuFile, new CpuFreq(cpuFile.getName(), minFreq, -1f, maxFreq));
}
Expand All @@ -81,8 +92,9 @@ public void run() {
for (File cpuFile : cachedFrequencies.keySet()) {
CpuFreq cached = cachedFrequencies.get(cpuFile);
double curFreq = -1f;
BufferedReader curFreqReader = null;
try {
BufferedReader curFreqReader = new BufferedReader(new FileReader(cpuFile.getAbsolutePath() + "/cpufreq/scaling_cur_freq"));
curFreqReader = new BufferedReader(new FileReader(cpuFile.getAbsolutePath() + "/cpufreq/scaling_cur_freq"));
curFreq = parseDouble(curFreqReader.readLine());

if (DebugOverlay.isDebugLoggingEnabled()) {
Expand All @@ -91,6 +103,12 @@ public void run() {

} catch (IOException ie) {
Log.w(TAG, "Error reading the current cpufreq", ie);
} finally {
if (curFreqReader != null) {
try {
curFreqReader.close();
} catch (IOException ignore) {}
}
}

newCpuFreqList.add(new CpuFreq(cached.getCpuName(), cached.getMinFreq(), curFreq, cached.getMaxFreq()));
Expand Down Expand Up @@ -144,15 +162,11 @@ protected List<CpuFreq> getLatestData() {

private static File[] getCpuFiles() {
File dir = new File("/sys/devices/system/cpu/");
File[] files = dir.listFiles(new FileFilter() {
return dir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
if(Pattern.matches("cpu[0-9]+", file.getName())) {
return true;
}
return false;
return Pattern.matches("cpu[0-9]+", file.getName());
}
});
return files;
}
}

0 comments on commit ffe516a

Please sign in to comment.