changeset 6422:55dc0f4c3579

8015334: Memory leak when kerning is used on Windows. Reviewed-by: srl, bae
author prr
date Tue, 25 Jun 2013 14:50:50 -0700
parents 262e23ebdc3d
children ee18d5c46468
files src/share/native/sun/font/layout/KernTable.cpp src/share/native/sun/font/layout/KernTable.h src/share/native/sun/font/layout/LayoutEngine.cpp test/java/awt/font/TextLayout/KerningLeak.java
diffstat 4 files changed, 80 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/native/sun/font/layout/KernTable.cpp	Fri Jun 21 19:01:58 2013 -0700
+++ b/src/share/native/sun/font/layout/KernTable.cpp	Tue Jun 25 14:50:50 2013 -0700
@@ -96,7 +96,7 @@
  * TODO: respect header flags
  */
 KernTable::KernTable(const LETableReference& base, LEErrorCode &success)
-  : pairs(), pairsSwapped(NULL), fTable(base)
+  : pairsSwapped(NULL), fTable(base)
 {
   if(LE_FAILURE(success) || (fTable.isEmpty())) {
 #if DEBUG
@@ -143,32 +143,36 @@
 #endif
 
         if(LE_SUCCESS(success) && nPairs>0) {
-          // pairs is an instance member, and table is on the stack.
-          // set 'pairs' based on table.getAlias(). This will range check it.
+          // pairsSwapped is an instance member, and table is on the stack.
+          // set 'pairsSwapped' based on table.getAlias(). This will range check it.
 
-          pairs = LEReferenceToArrayOf<PairInfo>(fTable, // based on overall table
-                                                 success,
-                                                 (const PairInfo*)table.getAlias(),  // subtable 0 + ..
-                                                 KERN_SUBTABLE_0_HEADER_SIZE,  // .. offset of header size
-                                                 nPairs); // count
-        }
-        if (LE_SUCCESS(success) && pairs.isValid()) {
-            pairsSwapped =  (PairInfo*)(malloc(nPairs*sizeof(PairInfo)));
-            PairInfo *p = (PairInfo*)pairsSwapped;
-            for (int i = 0; LE_SUCCESS(success) && i < nPairs; i++, p++) {
-              memcpy(p, pairs.getAlias(i,success), KERN_PAIRINFO_SIZE);
-              p->key = SWAPL(p->key);
+          pairsSwapped = (PairInfo*)(fTable.getFont()->getKernPairs());
+          if (pairsSwapped == NULL) {
+            LEReferenceToArrayOf<PairInfo>pairs =
+              LEReferenceToArrayOf<PairInfo>(fTable, // based on overall table
+                                             success,
+                                             (const PairInfo*)table.getAlias(),  // subtable 0 + ..
+                                             KERN_SUBTABLE_0_HEADER_SIZE,  // .. offset of header size
+                                             nPairs); // count
+            if (LE_SUCCESS(success) && pairs.isValid()) {
+              pairsSwapped =  (PairInfo*)(malloc(nPairs*sizeof(PairInfo)));
+              PairInfo *p = (PairInfo*)pairsSwapped;
+              for (int i = 0; LE_SUCCESS(success) && i < nPairs; i++, p++) {
+                memcpy(p, pairs.getAlias(i,success), KERN_PAIRINFO_SIZE);
+                p->key = SWAPL(p->key);
+              }
+              fTable.getFont()->setKernPairs((void*)pairsSwapped); // store it
             }
-            fTable.getFont()->setKernPairs((void*)pairsSwapped); // store it
+          }
         }
 
 #if 0
-        fprintf(stderr, "coverage: %0.4x nPairs: %d pairs %p\n", coverage, nPairs, pairs.getAlias());
+        fprintf(stderr, "coverage: %0.4x nPairs: %d pairs %p\n", coverage, nPairs, pairsSwapped);
         fprintf(stderr, "  searchRange: %d entrySelector: %d rangeShift: %d\n", searchRange, entrySelector, rangeShift);
         fprintf(stderr, "[[ ignored font table entries: range %d selector %d shift %d ]]\n", SWAPW(table->searchRange), SWAPW(table->entrySelector), SWAPW(table->rangeShift));
 #endif
 #if DEBUG
-        fprintf(stderr, "coverage: %0.4x nPairs: %d pairs 0x%x\n", coverage, nPairs, pairs);
+        fprintf(stderr, "coverage: %0.4x nPairs: %d pairs 0x%x\n", coverage, nPairs, pairsSwapped);
         fprintf(stderr,
           "  searchRange(pairs): %d entrySelector: %d rangeShift(pairs): %d\n",
           searchRange, entrySelector, rangeShift);
@@ -182,7 +186,7 @@
               ids[id] = (char)i;
             }
           }
-          PairInfo *p = pairs;
+          PairInfo *p = pairsSwapped;
           for (int i = 0; i < nPairs; ++i, p++) {
             le_uint32 k = p->key;
             le_uint16 left = (k >> 16) & 0xffff;
--- a/src/share/native/sun/font/layout/KernTable.h	Fri Jun 21 19:01:58 2013 -0700
+++ b/src/share/native/sun/font/layout/KernTable.h	Tue Jun 25 14:50:50 2013 -0700
@@ -57,7 +57,6 @@
  private:
   le_uint16 coverage;
   le_uint16 nPairs;
-  LEReferenceToArrayOf<PairInfo> pairs;
   PairInfo  *pairsSwapped;
   const LETableReference &fTable;
   le_uint16 searchRange;
--- a/src/share/native/sun/font/layout/LayoutEngine.cpp	Fri Jun 21 19:01:58 2013 -0700
+++ b/src/share/native/sun/font/layout/LayoutEngine.cpp	Tue Jun 25 14:50:50 2013 -0700
@@ -569,7 +569,6 @@
 {
   if(fGlyphStorage!=NULL) {
     fGlyphStorage->reset();
-    fGlyphStorage = NULL;
   }
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/font/TextLayout/KerningLeak.java	Tue Jun 25 14:50:50 2013 -0700
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 8015334
+ * @summary Memory leak with kerning.
+ */
+
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.font.FontRenderContext;
+import java.awt.font.TextAttribute;
+import java.awt.font.TextLayout;
+import java.util.HashMap;
+import java.util.Map;
+
+public class KerningLeak {
+
+    public static void main(String[] args) {
+        Map<TextAttribute, Object> textAttributes = new HashMap<>();
+        textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
+        textAttributes.put(TextAttribute.SIZE, 12);
+        textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
+        Font font = Font.getFont(textAttributes);
+        int totalAdvance = 0;
+        FontRenderContext frc = new FontRenderContext(null, false, false);
+        for (int i = 0; i < 500; i++) {
+            if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
+            for (int j = 0; j <1000; j++) {
+                TextLayout tl = new TextLayout(Integer.toString(j), font, frc);
+                totalAdvance += tl.getAdvance();
+            }
+        }
+        System.out.println("done " + totalAdvance);
+    }
+}