NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
Ace_Of_John
Joined: 21 Sep 2010 Posts: 1
|
Posted: Tue Sep 21, 2010 9:22 pm Post subject: leaking this in constructor |
|
|
The warning icon when i add this as the window listener says" Leaking this in constructer". im using IDE 6.9. im not sure if theres a reason for this but i cant find an explanation anywhere.
if anyone known i would be greatfull. Thankyou
| Code: |
public abstract class MainFrame extends Frame implements WindowListener
{
public MainFrame()
{
super();
addWindowListener(this);//<---Hear
}
public void windowOpened(WindowEvent e){}
...
}
|
|
|
| Back to top |
|
 |
Victor G. Vasilyev Posted via mailing list.
|
Posted: Sat Sep 25, 2010 10:19 pm Post subject: leaking this in constructor |
|
|
Hi Ace_Of_John,
If briefly then:
The warning is correct. Until done of initialization "this" object might
have incorrect state.
Hence, it is dangerous to publish reference to "this" object for outside
processing.
E.g. this is very important if either this class or called method is not
declared as final.
Note, this is a warning only, but not an error.
If you think that this hint is not useful then you can configure it via
the Options dialog:
Main Menu/Tools/Options/Editor/Hints
or press Alt-Enter on the statement underlined by the hint, choose the
"Configure ..." item, and unset the check mark at it.
Cheers,
Victor
P.S. If you know better pattern for the adding of a listener then,
please, publish it here ;-)
On 22.09.2010 1:23, Ace_Of_John wrote:
| Quote: | The warning icon when i add this as the window listener says" Leaking this in constructer". im using IDE 6.9. im not sure if theres a reason for this but i cant find an explanation anywhere.
if anyone known i would be greatfull. Thankyou
Code:
public abstract class MainFrame extends Frame implements WindowListener
{
public MainFrame()
{
super();
addWindowListener(this);//<---Hear
}
public void windowOpened(WindowEvent e){}
...
}
|
|
|
| Back to top |
|
 |
Owen Thomas
Joined: 20 Sep 2008 Posts: 147
|
Posted: Sun Sep 26, 2010 4:17 am Post subject: leaking this in constructor |
|
|
Although I find it a little hard at times not to use "this" in a constructor, I like this warning; at least it shows to me where the source of some potential (future) problems might occur.
Good work NetBeans. Some of your hints like using Collection.isEmpty instead of Collection.size()==0 are quite nice too.
On 26 September 2010 05:22, Victor G. Vasilyev <address-removed ([email]address-removed[/email])> wrote:
|
| Back to top |
|
 |
Alan Posted via mailing list.
|
Posted: Sun Sep 26, 2010 6:17 am Post subject: leaking this in constructor |
|
|
On Sat, Sep 25, 2010 at 8:18 PM, Owen Thomas <address-removed ([email]address-removed[/email])> wrote:
| Quote: |
Good work NetBeans. Some of your hints like using Collection.isEmpty instead of Collection.size()==0 are quite nice too.
| I was impressed with its ability to transform String catenation operations into the parametric equivalent for Logger messages. |
|
| Back to top |
|
 |
areeda
Joined: 28 Aug 2008 Posts: 469 Location: Los Angeles
|
Posted: Tue Sep 28, 2010 3:24 am Post subject: |
|
|
I've seen this warning and I have to admit I ignore it in cases like this.
I tend to create a single instance of frames and hide/show them as needed. So a memory leak really doesn't mean anything.
You can get rid of the warning by creating an "init" method and adding the listener there.
If you create and destroy this frame a lot something better needs to be done. |
|
| Back to top |
|
 |
Victor G. Vasilyev Posted via mailing list.
|
Posted: Tue Sep 28, 2010 7:45 pm Post subject: leaking this in constructor |
|
|
Hi areeda,
On 28.09.2010 7:25, areeda wrote:
| Quote: | So a memory leak really doesn't mean anything.
| Note, the warning is about the leaking of a reference to "this" object,
but not about a memory leak.
It has warned you to be careful in manipulation of non-completely
initialized object via the reference "this".
| Quote: | You can get rid of the warning by creating an "init" method and adding the listener there
| Unfortunately, such pattern will only hide the problem from the "expert
system", but not solve it.
Cheers,
Victor |
|
| Back to top |
|
 |
buzz3791
Joined: 14 Jul 2011 Posts: 1
|
Posted: Thu Jul 14, 2011 3:49 pm Post subject: 'This' escape warning is about thread safety not memory leak |
|
|
| areeda wrote: | | So a memory leak really doesn't mean anything. |
This warning is talking about concurrency / multithreading / thread safety not a memory leak. Chapter 3 "Sharing Objects", section 3.2 "Publication and escape", pp. 39-42 in the book titled Java Concurrency in Practice http://jcip.net/ discusses the warning
Do not allow the this reference to escape during construction.
The warning means in a constructor avoid using 'this' to register your class as listener. If you ignore the warning, another thread that works with the registered listeners can see your class before it is completely constructed.
The book suggests that if you want to register your newly constructed class as a listener use a private constructor and a public factory method like shown in the book's "Listing 3.8 Using a factory method to prevent the this reference from escaping during construction" on page 42. Here's a link to the example source code... http://jcip.net/listings/SafeListener.java
The book JCIP was written by Brian Goetz, Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea. I recommend it highly if you're going to do a lot of Swing programming.
The sections and page numbers I'm using are from the book's 6th printing March 2008.
Brian |
|
| Back to top |
|
 |
ptoye
Joined: 08 Dec 2009 Posts: 49 Location: Reading, England
|
Posted: Tue Aug 28, 2012 2:28 pm Post subject: Re: leaking this in constructor |
|
|
| Ace_Of_John wrote: | The warning icon when i add this as the window listener says" Leaking this in constructer". im using IDE 6.9. im not sure if theres a reason for this but i cant find an explanation anywhere.
if anyone known i would be greatfull. Thankyou
| Code: |
public abstract class MainFrame extends Frame implements WindowListener
{
public MainFrame()
{
super();
addWindowListener(this);//<---Hear
}
public void windowOpened(WindowEvent e){}
...
}
|
|
Sorry not to have replied to this before, but I'm having the same problem (with NB 7.2).
As has been said, the warning is genuine. What would happen if a WindowEvent happened before your constructor had finished initialising? The window event handler might start doing things to the MainFrame which isn't really there yet.
I've noticed that Netbeans itself generates very similar code when you attach an event handler to a component in the frame, except that it generates a new anonymous listener which then calls the event handler. This gets round the warning message, but to my simple mind doesn't get round the problem.
I suppose one way round the problem is to have an attacher method which attaches the listener(s) and to remember to call it immediately after each time the class is instantiated. But then what happens if the event happens between the constructor call and the attacher call? Would putting this call as the last line in the constructor work?
Is there an expert here who can help? |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|
|