Skip to content

Commit 7f69191

Browse files
committed
[#693] Fix resource leak warnings for org.eclipse.cdt.core.utils
* use `try-with-resources` for `AutoCloseable`
1 parent 2e38e74 commit 7f69191

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/dwarf/Dwarf.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2023 QNX Software Systems and others.
2+
* Copyright (c) 2000, 2024 QNX Software Systems and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -14,6 +14,7 @@
1414
* Serge Beauchamp - Bug 409916
1515
* John Dallaway - Support DW_FORM_line_strp (#198)
1616
* John Dallaway - Support DW_FORM_implicit_const (#443)
17+
* Alexander Fedorov (ArSysOp) - fix resource leak (#693)
1718
*******************************************************************************/
1819

1920
package org.eclipse.cdt.utils.debug.dwarf;
@@ -1065,12 +1066,10 @@ void processCompileUnit(IDebugEntryRequestor requestor, List<AttributeValue> lis
10651066
}
10661067

10671068
public static void main(String[] args) {
1068-
try {
1069-
DebugSymsRequestor symreq = new DebugSymsRequestor();
1070-
Dwarf dwarf = new Dwarf(args[0]);
1069+
DebugSymsRequestor symreq = new DebugSymsRequestor();
1070+
try (Dwarf dwarf = new Dwarf(args[0])) {
10711071
dwarf.parse(symreq);
1072-
DebugSym[] entries = symreq.getEntries();
1073-
for (DebugSym entry : entries) {
1072+
for (DebugSym entry : symreq.getEntries()) {
10741073
System.out.println(entry);
10751074
}
10761075
} catch (IOException e) {

core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugAddr2line.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2006 QNX Software Systems and others.
2+
* Copyright (c) 2000, 2024 QNX Software Systems and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
1010
*
1111
* Contributors:
1212
* QNX Software Systems - Initial API and implementation
13+
* Alexander Fedorov (ArSysOp) - fix resource leak (#693)
1314
*******************************************************************************/
1415

1516
package org.eclipse.cdt.utils.debug.tools;
@@ -48,8 +49,9 @@ void init(Elf elf) throws IOException {
4849
Stabs stabs = new Stabs(elf);
4950
stabs.parse(symreq);
5051
} else if (type == Elf.Attribute.DEBUG_TYPE_DWARF) {
51-
Dwarf dwarf = new Dwarf(elf);
52-
dwarf.parse(symreq);
52+
try (Dwarf dwarf = new Dwarf(elf)) {
53+
dwarf.parse(symreq);
54+
}
5355
} else {
5456
throw new IOException(CCorePlugin.getResourceString("Util.unknownFormat")); //$NON-NLS-1$
5557
}

core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/debug/tools/DebugDump.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2016 QNX Software Systems and others.
2+
* Copyright (c) 2000, 2024 QNX Software Systems and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
1010
*
1111
* Contributors:
1212
* QNX Software Systems - Initial API and implementation
13+
* Alexander Fedorov (ArSysOp) - fix resource leak (#693)
1314
*******************************************************************************/
1415

1516
package org.eclipse.cdt.utils.debug.tools;
@@ -58,8 +59,9 @@ void parse(Elf elf) throws IOException {
5859
Stabs stabs = new Stabs(elf);
5960
stabs.parse(this);
6061
} else if (type == Elf.Attribute.DEBUG_TYPE_DWARF) {
61-
Dwarf dwarf = new Dwarf(elf);
62-
dwarf.parse(this);
62+
try (Dwarf dwarf = new Dwarf(elf)) {
63+
dwarf.parse(this);
64+
}
6365
} else {
6466
throw new IOException(CCorePlugin.getResourceString("Util.unknownFormat")); //$NON-NLS-1$
6567
}

core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/macho/MachO64.java

+10-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2016 QNX Software Systems and others.
2+
* Copyright (c) 2000, 2024 QNX Software Systems and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -12,6 +12,7 @@
1212
* QNX Software Systems - Initial API and implementation
1313
* Craig Watson.
1414
* Apple Computer - work on performance optimizations
15+
* Alexander Fedorov (ArSysOp) - fix resource leak (#693)
1516
*******************************************************************************/
1617
package org.eclipse.cdt.utils.macho;
1718

@@ -1157,20 +1158,17 @@ public Attribute getAttributes() throws IOException {
11571158
}
11581159

11591160
public static Attribute getAttributes(String file) throws IOException {
1160-
MachO64 macho = new MachO64(file);
1161-
Attribute attrib = macho.getAttributes();
1162-
macho.dispose();
1163-
return attrib;
1161+
try (MachO64 macho = new MachO64(file)) {
1162+
return macho.getAttributes();
1163+
}
11641164
}
11651165

11661166
public static Attribute getAttributes(byte[] array) throws IOException {
1167-
MachO64 emptyMachO = new MachO64();
1168-
emptyMachO.mhdr = emptyMachO.new MachOhdr(array);
1169-
//emptyMachO.sections = new MachO64.Section[0];
1170-
Attribute attrib = emptyMachO.getAttributes();
1171-
emptyMachO.dispose();
1172-
1173-
return attrib;
1167+
try (MachO64 emptyMachO = new MachO64()) {
1168+
emptyMachO.mhdr = emptyMachO.new MachOhdr(array);
1169+
//emptyMachO.sections = new MachO64.Section[0];
1170+
return emptyMachO.getAttributes();
1171+
}
11741172
}
11751173

11761174
public static boolean isMachOHeader(byte[] bytes) {

0 commit comments

Comments
 (0)