Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4308367

WClipboard.setClipboardText() has memory leak (need 1.1.x fix)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.1.7
    • client-libs
    • x86
    • windows_nt



      Name: krT82822 Date: 01/29/2000


      WClipboard.setClipboardText() has a memory leak. This exists in the 1.1 JDK's and 1.2.2,
      but appears to have been fixed in 1.3 (though I can't find a bug number for it). Should be
      an easy fix for 1.1.9.
      Basically, the native code for this method in awt_clipboard.cpp creates a temporary string
      to hold the text, but never frees it. For large strings this can be a massive memory leak.
      A simple test case to illustrate the problem follows. If you run this, you will see the memory
      usage skyrocket. Warning: running this test case will quickly blow out your system's virtual
      memory and could cause things to crash:

      import java.awt.datatransfer.Clipboard;
      import java.awt.datatransfer.ClipboardOwner;
      import java.awt.datatransfer.StringSelection;
      import java.awt.datatransfer.Transferable;
      import java.awt.Toolkit;
      public class CBTest implements ClipboardOwner {
          public static void main(String[] args) {
              new CBTest();
          }
          public CBTest() {
              // Get System Clipboard
              Clipboard sysCB =
                  Toolkit.getDefaultToolkit().getSystemClipboard();
              // Create a very long string
              String str = "";
              for (int i = 0; i < 100; i++) {
              str += "This is a very long string to illustrate the leak";
              }
              // Put the string on the clipboard
              StringSelection sel = new StringSelection(str);
              while (true) {
                  sysCB.setContents(sel, this);
              }
          }
          public void lostOwnership(Clipboard clipboard,
                                    Transferable contents) {
          }
      }

      The fix is very easy, simply add the statement "delete[] ucText;" to the end of the function.
      Though this may seem like a minor leak, we have some products that use the clipboard for
      transfering database information to other applications, like spreadsheets. This data can be
      very large.
      (Review ID: 100372)
      ======================================================================

            lbunnisunw Lara Bunni (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: