JDK-4261876 : charWidth() method in FontMetrics leaks memory and causes JVM to crash
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 1.2.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 1999-08-11
  • Updated: 1999-10-26
  • Resolved: 1999-10-26
Related Reports
Duplicate :  
Description

Name: skT88420			Date: 08/11/99


The method charWidth(char c) in FontMetrics (more precisely a subclass sun.awt.font.FontDesignMetrics) has a memory leak and causes the Java Virtual Machine to crash after dereferencing memory address zero.

The size of the memory leak, and the number of calls it takes to provoke the crash is affected by the scale factor applied by the current AffineTransform.

Running on Windows NT 4.0 Service Pack 5
Pentium II 300MHz with 128MB RAM

C:\JBugs>java -version
java version "1.2.2"
Classic VM (build JDK-1.2.2-W, native threads, symcjit)

C:\JBugs>java -fullversion
java full version "JDK-1.2.2-W"

Source code:

import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;

import javax.swing.*;

public class TestFonts
{
    public static void main(String[] args)
    {
        Image buffer = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D) buffer.getGraphics();

        double scale = 0.004;   // *** Should get to char: d code: 100 then crash
        // scale = 0.001;       // *** Should get to char: $ code: 36 then crash with this line uncommented

        AffineTransform at = new AffineTransform(scale, 0.0, 0.0, scale, 0.0, 0.0);
        g2.transform(at);

        int textPoints = 12;
        Font font = new Font("Times New Roman", Font.PLAIN, (int) (textPoints / scale + 0.5));

        System.out.print(font.getFontName() + " : ");
        System.out.print(font.getFamily() + " : ");
        System.out.print(font.getName());
        System.out.println();

        getCharWidths(g2, font);

        g2.dispose();
    }

    private static void getCharWidths(Graphics2D g2, Font font)
    {
        FontMetrics fm = g2.getFontMetrics(font);

        int minChar = 32;  // Skip the non-printing characters
        int maxChar = 256; // For 8 bit character sets

        for (long i = minChar; i < maxChar; i++)
        {
            char c = (char) i;

            if (font.canDisplay(c))
            {
                System.out.println("Character: " + c + " Code: " + (int) c + " Width: " + fm.charWidth(c));
            }
        }

        System.out.println("**********************************");
    }
}
(Review ID: 93762) 
======================================================================