Skip to content

Commit 2152227

Browse files
committed
fixing more resource leaks in loadJSONObject() and loadJSONArray()
1 parent 0621265 commit 2152227

2 files changed

Lines changed: 82 additions & 47 deletions

File tree

core/src/processing/core/PApplet.java

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6076,14 +6076,31 @@ public JSONObject parseJSONObject(String input) {
60766076
* @see PApplet#saveJSONArray(JSONArray, String)
60776077
*/
60786078
public JSONObject loadJSONObject(String filename) {
6079-
return new JSONObject(createReader(filename));
6079+
// can't pass of createReader() to the constructor b/c of resource leak
6080+
BufferedReader reader = createReader(filename);
6081+
JSONObject outgoing = new JSONObject(reader);
6082+
try {
6083+
reader.close();
6084+
} catch (IOException e) { // not sure what would cause this
6085+
e.printStackTrace();
6086+
}
6087+
return outgoing;
60806088
}
60816089

6090+
60826091
/**
60836092
* @nowebref
60846093
*/
60856094
static public JSONObject loadJSONObject(File file) {
6086-
return new JSONObject(createReader(file));
6095+
// can't pass of createReader() to the constructor b/c of resource leak
6096+
BufferedReader reader = createReader(file);
6097+
JSONObject outgoing = new JSONObject(reader);
6098+
try {
6099+
reader.close();
6100+
} catch (IOException e) { // not sure what would cause this
6101+
e.printStackTrace();
6102+
}
6103+
return outgoing;
60876104
}
60886105

60896106

@@ -6101,6 +6118,7 @@ public boolean saveJSONObject(JSONObject json, String filename) {
61016118
return saveJSONObject(json, filename, null);
61026119
}
61036120

6121+
61046122
/**
61056123
* @param options "compact" and "indent=N", replace N with the number of spaces
61066124
*/
@@ -6129,12 +6147,28 @@ public JSONArray parseJSONArray(String input) {
61296147
* @see PApplet#saveJSONArray(JSONArray, String)
61306148
*/
61316149
public JSONArray loadJSONArray(String filename) {
6132-
return new JSONArray(createReader(filename));
6150+
// can't pass of createReader() to the constructor b/c of resource leak
6151+
BufferedReader reader = createReader(filename);
6152+
JSONArray outgoing = new JSONArray(reader);
6153+
try {
6154+
reader.close();
6155+
} catch (IOException e) { // not sure what would cause this
6156+
e.printStackTrace();
6157+
}
6158+
return outgoing;
61336159
}
61346160

61356161

61366162
static public JSONArray loadJSONArray(File file) {
6137-
return new JSONArray(createReader(file));
6163+
// can't pass of createReader() to the constructor b/c of resource leak
6164+
BufferedReader reader = createReader(file);
6165+
JSONArray outgoing = new JSONArray(reader);
6166+
try {
6167+
reader.close();
6168+
} catch (IOException e) { // not sure what would cause this
6169+
e.printStackTrace();
6170+
}
6171+
return outgoing;
61386172
}
61396173

61406174

@@ -12126,11 +12160,11 @@ public void rect(float a, float b, float c, float d,
1212612160
/**
1212712161
* ( begin auto-generated from square.xml )
1212812162
*
12129-
* Draws a square to the screen. A square is a four-sided shape with
12130-
* every angle at ninety degrees and each side is the same length.
12131-
* By default, the first two parameters set the location of the
12132-
* upper-left corner, the third sets the width and height. The way
12133-
* these parameters are interpreted, however, may be changed with the
12163+
* Draws a square to the screen. A square is a four-sided shape with
12164+
* every angle at ninety degrees and each side is the same length.
12165+
* By default, the first two parameters set the location of the
12166+
* upper-left corner, the third sets the width and height. The way
12167+
* these parameters are interpreted, however, may be changed with the
1213412168
* <b>rectMode()</b> function.
1213512169
*
1213612170
* ( end auto-generated )
@@ -12239,9 +12273,9 @@ public void arc(float a, float b, float c, float d,
1223912273
/**
1224012274
* ( begin auto-generated from circle.xml )
1224112275
*
12242-
* Draws a circle to the screen. By default, the first two parameters
12243-
* set the location of the center, and the third sets the shape's width
12244-
* and height. The origin may be changed with the <b>ellipseMode()</b>
12276+
* Draws a circle to the screen. By default, the first two parameters
12277+
* set the location of the center, and the third sets the shape's width
12278+
* and height. The origin may be changed with the <b>ellipseMode()</b>
1224512279
* function.
1224612280
*
1224712281
* ( end auto-generated )
@@ -13283,28 +13317,28 @@ public void text(float num, float x, float y, float z) {
1328313317
/**
1328413318
* ( begin auto-generated from push.xml )
1328513319
*
13286-
* The <b>push()</b> function saves the current drawing style
13287-
* settings and transformations, while <b>pop()</b> restores these
13288-
* settings. Note that these functions are always used together.
13289-
* They allow you to change the style and transformation settings
13290-
* and later return to what you had. When a new state is started
13291-
* with push(), it builds on the current style and transform
13320+
* The <b>push()</b> function saves the current drawing style
13321+
* settings and transformations, while <b>pop()</b> restores these
13322+
* settings. Note that these functions are always used together.
13323+
* They allow you to change the style and transformation settings
13324+
* and later return to what you had. When a new state is started
13325+
* with push(), it builds on the current style and transform
1329213326
* information.<br />
1329313327
* <br />
13294-
* <b>push()</b> stores information related to the current
13295-
* transformation state and style settings controlled by the
13296-
* following functions: <b>rotate()</b>, <b>translate()</b>,
13297-
* <b>scale()</b>, <b>fill()</b>, <b>stroke()</b>, <b>tint()</b>,
13298-
* <b>strokeWeight()</b>, <b>strokeCap()</b>, <b>strokeJoin()</b>,
13299-
* <b>imageMode()</b>, <b>rectMode()</b>, <b>ellipseMode()</b>,
13300-
* <b>colorMode()</b>, <b>textAlign()</b>, <b>textFont()</b>,
13328+
* <b>push()</b> stores information related to the current
13329+
* transformation state and style settings controlled by the
13330+
* following functions: <b>rotate()</b>, <b>translate()</b>,
13331+
* <b>scale()</b>, <b>fill()</b>, <b>stroke()</b>, <b>tint()</b>,
13332+
* <b>strokeWeight()</b>, <b>strokeCap()</b>, <b>strokeJoin()</b>,
13333+
* <b>imageMode()</b>, <b>rectMode()</b>, <b>ellipseMode()</b>,
13334+
* <b>colorMode()</b>, <b>textAlign()</b>, <b>textFont()</b>,
1330113335
* <b>textMode()</b>, <b>textSize()</b>, <b>textLeading()</b>.<br />
1330213336
* <br />
13303-
* The <b>push()</b> and <b>pop()</b> functions were added with
13304-
* Processing 3.5. They can be used in place of <b>pushMatrix()</b>,
13305-
* <b>popMatrix()</b>, <b>pushStyles()</b>, and <b>popStyles()</b>.
13306-
* The difference is that push() and pop() control both the
13307-
* transformations (rotate, scale, translate) and the drawing styles
13337+
* The <b>push()</b> and <b>pop()</b> functions were added with
13338+
* Processing 3.5. They can be used in place of <b>pushMatrix()</b>,
13339+
* <b>popMatrix()</b>, <b>pushStyles()</b>, and <b>popStyles()</b>.
13340+
* The difference is that push() and pop() control both the
13341+
* transformations (rotate, scale, translate) and the drawing styles
1330813342
* at the same time.
1330913343
*
1331013344
* ( end auto-generated )
@@ -13321,28 +13355,28 @@ public void push() {
1332113355
/**
1332213356
* ( begin auto-generated from pop.xml )
1332313357
*
13324-
* The <b>pop()</b> function restores the previous drawing style
13325-
* settings and transformations after <b>push()</b> has changed them.
13326-
* Note that these functions are always used together. They allow
13327-
* you to change the style and transformation settings and later
13328-
* return to what you had. When a new state is started with push(),
13358+
* The <b>pop()</b> function restores the previous drawing style
13359+
* settings and transformations after <b>push()</b> has changed them.
13360+
* Note that these functions are always used together. They allow
13361+
* you to change the style and transformation settings and later
13362+
* return to what you had. When a new state is started with push(),
1332913363
* it builds on the current style and transform information.<br />
1333013364
* <br />
1333113365
* <br />
13332-
* <b>push()</b> stores information related to the current
13333-
* transformation state and style settings controlled by the
13334-
* following functions: <b>rotate()</b>, <b>translate()</b>,
13335-
* <b>scale()</b>, <b>fill()</b>, <b>stroke()</b>, <b>tint()</b>,
13336-
* <b>strokeWeight()</b>, <b>strokeCap()</b>, <b>strokeJoin()</b>,
13337-
* <b>imageMode()</b>, <b>rectMode()</b>, <b>ellipseMode()</b>,
13338-
* <b>colorMode()</b>, <b>textAlign()</b>, <b>textFont()</b>,
13366+
* <b>push()</b> stores information related to the current
13367+
* transformation state and style settings controlled by the
13368+
* following functions: <b>rotate()</b>, <b>translate()</b>,
13369+
* <b>scale()</b>, <b>fill()</b>, <b>stroke()</b>, <b>tint()</b>,
13370+
* <b>strokeWeight()</b>, <b>strokeCap()</b>, <b>strokeJoin()</b>,
13371+
* <b>imageMode()</b>, <b>rectMode()</b>, <b>ellipseMode()</b>,
13372+
* <b>colorMode()</b>, <b>textAlign()</b>, <b>textFont()</b>,
1333913373
* <b>textMode()</b>, <b>textSize()</b>, <b>textLeading()</b>.<br />
1334013374
* <br />
13341-
* The <b>push()</b> and <b>pop()</b> functions were added with
13342-
* Processing 3.5. They can be used in place of <b>pushMatrix()</b>,
13343-
* <b>popMatrix()</b>, <b>pushStyles()</b>, and <b>popStyles()</b>.
13344-
* The difference is that push() and pop() control both the
13345-
* transformations (rotate, scale, translate) and the drawing styles
13375+
* The <b>push()</b> and <b>pop()</b> functions were added with
13376+
* Processing 3.5. They can be used in place of <b>pushMatrix()</b>,
13377+
* <b>popMatrix()</b>, <b>pushStyles()</b>, and <b>popStyles()</b>.
13378+
* The difference is that push() and pop() control both the
13379+
* transformations (rotate, scale, translate) and the drawing styles
1334613380
* at the same time.
1334713381
*
1334813382
* ( end auto-generated )

core/todo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
0269 (3.5.3)
22
X deal with possible resource leak when loading URLs
33
X create intermediate folders when saving temp files
4+
X fix resource leaks (open files) in loadJSONObject and loadJSONArray
45

56

67
contrib

0 commit comments

Comments
 (0)