Skip to content

Commit 2971201

Browse files
FOP-3269: Allow GC to remove structelem tree
1 parent 71e5bfc commit 2971201

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

fop-core/src/main/java/org/apache/fop/pdf/PDFStructElem.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,16 @@ public int output(OutputStream stream) throws IOException {
283283
}
284284
}
285285
}
286-
return super.output(stream);
286+
int len = super.output(stream);
287+
close();
288+
return len;
289+
}
290+
291+
private void close() {
292+
parent = null;
293+
parentElement = null;
294+
entries = null;
295+
kids = null;
287296
}
288297

289298
private boolean isBSLE(PDFStructElem kid) {

fop-core/src/test/java/org/apache/fop/accessibility/pdf/FootnoteSeparatorTestCase.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.xml.sax.helpers.AttributesImpl;
2828
import static org.junit.Assert.assertEquals;
2929

30-
import org.apache.fop.pdf.PDFArray;
3130
import org.apache.fop.pdf.PDFDocument;
3231
import org.apache.fop.pdf.PDFFactory;
3332
import org.apache.fop.pdf.PDFParentTree;
@@ -39,7 +38,7 @@ public class FootnoteSeparatorTestCase {
3938

4039
@Test
4140
public void testFootNoteSeparatorText() throws IOException {
42-
PDFParentTree tree = new PDFParentTree();
41+
PDFParentTree tree = new PDFParentTree();
4342
AttributesImpl attributes = new AttributesImpl();
4443
attributes.addAttribute("", "role", "role", "CDATA", null);
4544
PDFDocument doc = new PDFDocument("");
@@ -49,20 +48,16 @@ public void testFootNoteSeparatorText() throws IOException {
4948
factory, null);
5049
AttributesImpl att = new AttributesImpl();
5150
att.addAttribute("", "flow-name", "flow-name", "CDATA", "xsl-footnote-separator");
52-
PDFStructElem staticSection = PDFStructureTreeBuilder.createStructureElement("static-content", part, att,
51+
PDFStructureTreeBuilder.createStructureElement("static-content", part, att,
5352
factory, null);
54-
PDFStructElem block = PDFStructureTreeBuilder.createStructureElement("block", part, new AttributesImpl(),
53+
PDFStructureTreeBuilder.createStructureElement("block", part, new AttributesImpl(),
5554
factory, null);
5655
ByteArrayOutputStream bos = new ByteArrayOutputStream();
5756
part.output(bos);
58-
PDFArray array = (PDFArray)part.get("K");
59-
PDFStructElem elem1 = (PDFStructElem)array.get(0);
60-
String test = elem1.getStructureType().getName().getName();
61-
String expected = "P";
62-
assertEquals(test, expected);
63-
PDFStructElem elem2 = (PDFStructElem)array.get(1);
64-
test = elem2.getStructureType().getName().getName();
65-
expected = "Sect";
66-
assertEquals(test, expected);
57+
assertEquals(bos.toString(), "<<\n"
58+
+ " /S /Part\n"
59+
+ " /P 1 0 R\n"
60+
+ " /K [<< /S /P /P null >> << /S /Sect /P null >>]\n"
61+
+ ">>");
6762
}
6863
}

fop-core/src/test/java/org/apache/fop/render/pdf/PDFStructureTreeBuilderTestCase.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.Test;
2727

2828
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertNull;
2930

3031
import org.apache.fop.pdf.PDFDocument;
3132
import org.apache.fop.pdf.PDFFactory;
@@ -36,6 +37,11 @@
3637
public class PDFStructureTreeBuilderTestCase {
3738
private PDFFactory pdfFactory;
3839

40+
@Before
41+
public void setUp() {
42+
PDFDocument doc = new PDFDocument("");
43+
pdfFactory = new PDFFactory(doc);
44+
}
3945
@Test
4046
public void testAddImageContentItem() throws IOException {
4147
PDFStructElem structElem = new PDFStructElem(null, StandardStructureTypes.Illustration.FIGURE);
@@ -52,9 +58,13 @@ public void testAddImageContentItem() throws IOException {
5258
+ ">>] >>");
5359
}
5460

55-
@Before
56-
public void setUp() {
57-
PDFDocument doc = new PDFDocument("");
58-
pdfFactory = new PDFFactory(doc);
61+
@Test
62+
public void testMemoryLeak() throws IOException {
63+
PDFStructElem structElem = new PDFStructElem(new PDFStructElem(), StandardStructureTypes.Table.TABLE);
64+
structElem.addKid(new PDFStructElem());
65+
structElem.output(new ByteArrayOutputStream());
66+
assertNull(structElem.getParent());
67+
assertNull(structElem.getParentStructElem());
68+
assertNull(structElem.getKids());
5969
}
6070
}

0 commit comments

Comments
 (0)