NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

leaking this in constructor

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
Ace_Of_John



Joined: 21 Sep 2010
Posts: 1

PostPosted: Tue Sep 21, 2010 9:22 pm    Post subject: leaking this in constructor Reply with 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
Victor G. Vasilyev
Posted via mailing list.





PostPosted: Sat Sep 25, 2010 10:19 pm    Post subject: leaking this in constructor Reply with quote

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

PostPosted: Sun Sep 26, 2010 4:17 am    Post subject: leaking this in constructor Reply with quote

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. Smile

On 26 September 2010 05:22, Victor G. Vasilyev <address-removed ([email]address-removed[/email])> wrote:
Quote:
Back to top
Alan
Posted via mailing list.





PostPosted: Sun Sep 26, 2010 6:17 am    Post subject: leaking this in constructor Reply with quote

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. Smile



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

PostPosted: Tue Sep 28, 2010 3:24 am    Post subject: Reply with quote

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.





PostPosted: Tue Sep 28, 2010 7:45 pm    Post subject: leaking this in constructor Reply with quote

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

PostPosted: Thu Jul 14, 2011 3:49 pm    Post subject: 'This' escape warning is about thread safety not memory leak Reply with quote

areeda wrote:
So a memory leak really doesn't mean anything.


Shocked

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

PostPosted: Tue Aug 28, 2012 2:28 pm    Post subject: Re: leaking this in constructor Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo