Hi,

I have a very simple test program. A button to add an object(Sphere) every
time, another button to delete an object every time. If I deleted all the
objects that I added, I suppose there should no BranchGroup, Sphere,
Material objects left in the memory if there is no memory leak. But in the
 snapshot that I took using JProbe, there is still one instance of each
 those objects in the memory. The reference graph shows like:
      SimpleUniverser---SetLiveState---ArrayList....BranchGroup
I don't know why there is such a reference graph, and I think I can do
nothing to cut this reference path. Is this a memory leak bug or what?
I welcome your comments and Thanks a lot. ---white

Java3D 1.3.1 OpenGL version is used.

/*
 * MemoryTest2.java
 *
 * Created on June 23, 2003, 4:50 PM
 */
import javax.vecmath.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.picking.PickTool;

import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
import com.sun.j3d.utils.universe.PlatformGeometry;
import com.sun.j3d.utils.geometry.*;

public class MemoryTest2 extends javax.swing.JFrame {
    private SimpleUniverse universe;
    private int numObjects = 0;
    private BranchGroup[] objects = new BranchGroup[40];

    /** Creates new form MemoryTest */
    public MemoryTest2() {
        initComponents();
        init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        addButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();

        getContentPane().setLayout(new java.awt.BorderLayout(10, 20));

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 15, 5));

        addButton.setText("Add Object");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        jPanel1.add(addButton);

        deleteButton.setText("Delete Object");
        deleteButton.setEnabled(false);
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        jPanel1.add(deleteButton);

        jLabel1.setText("Number of Objects:");
        jPanel1.add(jLabel1);

        jTextField1.setEditable(false);
        jTextField1.setText(Integer.toString(numObjects));
        jPanel1.add(jTextField1);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        jPanel2.setLayout(new java.awt.BorderLayout());

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
        java.awt.Dimension screenSize = 
java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setSize(new java.awt.Dimension(640, 480));
        setLocation((screenSize.width-640)/2,(screenSize.height-480)/2);
    }

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:
        universe.getLocale().removeBranchGraph(objects[--numObjects]);
        objects[numObjects] = null;
        com.sun.j3d.utils.universe.Viewer.clearViewerMap();

        jTextField1.setText(Integer.toString(numObjects));
        if (numObjects == 0)
            deleteButton.setEnabled(false);

        if (numObjects < 30)
            addButton.setEnabled(true);

        //universe.cleanup();
        jPanel2.updateUI();

        System.gc();
    }

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
        // Add your handling code here:

        BranchGroup object = createGraph();
        objects[numObjects++] = object;
        universe.addBranchGraph(object);

        jTextField1.setText(Integer.toString(numObjects));
        if (numObjects >= 30)
            addButton.setEnabled(false);

        if (numObjects > 0)
            deleteButton.setEnabled(true);

        jPanel2.updateUI();
    }

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }


    private void init() {
        Canvas3D c3d = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        universe = new SimpleUniverse(c3d);
        Transform3D t = new Transform3D();
        t.set(new Vector3d(0.0, 0.0, 5.0));
        ((universe.getViewingPlatform()).getViewPlatformTransform()).setTransform(t);

        OrbitBehavior orbit = new OrbitBehavior(c3d, OrbitBehavior.REVERSE_ALL);
        orbit.setSchedulingBounds(new BoundingSphere(new Point3d(), 100));
        universe.getViewingPlatform().setViewPlatformBehavior(orbit);

        jPanel2.add(c3d, java.awt.BorderLayout.CENTER);

        PlatformGeometry pg = new PlatformGeometry();

        DirectionalLight light1 = new DirectionalLight(true, new Color3f(1.0f,
                                1.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f));
        light1.setCapability(DirectionalLight.ALLOW_DIRECTION_WRITE);

        Bounds bound = new BoundingSphere(new Point3d(0, 0, 0), 100);
        light1.setInfluencingBounds(bound);

        AmbientLight light2 = new AmbientLight(true, new Color3f(0.3f, 0.3f, 0.3f));
        light2.setInfluencingBounds(bound);

        pg.addChild(light1);
        pg.addChild(light2);

        universe.getViewingPlatform().setPlatformGeometry(pg);
    }


    private BranchGroup createGraph() {
        BranchGroup root = new BranchGroup();
        root.setCapability(BranchGroup.ALLOW_DETACH);

 Sphere sphere = new Sphere();

 Material myMaterial = new Material();
        myMaterial.setCapability(Material.ALLOW_COMPONENT_WRITE);
        myMaterial.setAmbientColor(0.2f, 0.2f, 0.2f);
        myMaterial.setDiffuseColor(0.6f, 0.3f, 0.2f);
        myMaterial.setShininess(15.0f);
        myMaterial.setLightingEnable(true);

        Appearance app = new Appearance();
        app.setMaterial(myMaterial);
 sphere.setAppearance(app);

        root.addChild(sphere);
        root.compile();

        return root;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new MemoryTest2().show();
    }


    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton deleteButton;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton addButton;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration

}

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to