aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Is it a source of bug? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is it a source of bug?" Watch "Is it a source of bug?" New topic
Author

Is it a source of bug?

Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
here is a piece of code




My question is that the reference b is assigned Button object three times...and the first two buttons "Hello" and "OK" are not referenced by any references..
Now as they are not referenced by any reference, so garbage collector may collects its memory, resulting in loss of these two buttons.....
Am I right or not???

Please comment......


Thanks

Living is easy with your eyes closed.....
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10523
    
    9

Did you try running your code? How many buttons do you see on the UI. What happens when you click the buttons?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you *sure* nothing references them? Does anything *else* reference them?
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
it shows three buttons.....and yes there is no reference for first two buttons....

for your simplicity run this one....

import java.awt.*;
import java.awt.event.*;

public class Activator {
public static void main(String[] args) {
Button b;
Frame f = new Frame("Hello Java");
f.add(b = new Button("Hello"),BorderLayout.NORTH);
f.add(b = new Button("OK"), BorderLayout.CENTER);
f.add(b = new Button("Quit"),BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}



Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
in the latest code I removed the event handling.....in UI there are three buttons and when i click on them nothing happen...the buttons are still present...

and in former code where I have used event handling everything is working fine........


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The Frame that contains them is referencing them. When you call add(), the Frame adds the Button to a collection; that collection keeps the Button from being garbage collected. The only reason for using the variable "b" in the original code is so that the event listener can be added to the button.


[Jess in Action][AskingGoodQuestions]
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
This means that I only have to take care of the Frame, If I put the null value in the frame object
i.e.

f=null;


at the closing of main method, the again it is visible and every thing works fine why???
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Because the event queue (and perhaps other internal parts of the GUI) has a reference to the Frame. To null out all those references you have to call dispose() on the Frame; then it can be garbage-collected.
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
thanks for your reply
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 650
What you may be missing here is that you're creating components for use by the AWT. When you call setVisible on the frame, the AWT event thread takes over and manages the application.

Have you wondered what happens to the application when the main() method is finished? Perhaps you haven't noticed, but that method finishes and the original thread that started it is done. The application then run via the AWT event thread.

I mention this because of your comment that one of your 'b' variables was still set. In actuality, after main() ends, all the local variables are out of scope (both 'b' and 'f').

By the way, for a simple test application, it's okay to create graphical components in the main application thread. However, you should get in the habit of creating all graphical components in the AWT event thread, using EventQueue.invokeLater(...); Here is an example:
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
Who is the parent thread of Awt event thread, JVM or main method.....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Unknown. A little investigation has shown me that the EDT is member of the main thread group, of which the main thread is also a member. That doesn't necessarily mean that the main thread is also the parent thread - there are a few other member threads. But does it really matter?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it a source of bug?