This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 231005 - could you clarify please NodeList.removeNotify behaviour
Summary: could you clarify please NodeList.removeNotify behaviour
Status: RESOLVED FIXED
Alias: None
Product: projects
Classification: Unclassified
Component: Generic Projects UI (show other bugs)
Version: 7.3
Hardware: All All
: P2 normal (vote)
Assignee: Milos Kleint
URL:
Keywords:
Depends on:
Blocks: 230378
  Show dependency tree
 
Reported: 2013-06-10 05:24 UTC by David Konecny
Modified: 2013-06-12 02:02 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Konecny 2013-06-10 05:24:50 UTC
I'm fixing a memory leak in HTML5 project type (issue 230378) and one of the problems (there is more of them) is that removeNotify is never called on my subclass of org.netbeans.spi.project.ui.support.NodeList (org.netbeans.modules.web.clientproject.ui.ClientSideProjectLogicalView.ClientProjectNodeList) and consequently the cleanup of listeners is never performed and result is a memory leak.

The Javadoc for NodeList.removeNotify says:

     * Called when the node list is no longer needed. Equivalent 
     * to {@link org.openide.nodes.Children#removeNotify}.
     * Unregister any listeners and perform any general cleanup.

which is what I would expect. Partially because of the method name is identical to JComponent.removeNotify. However Childer.removeNotify says in its Javadoc:

    /** Called when all the children Nodes are freed from memory.
     * Typical implementations at this time clear all the keys
     * (in case of {@link Children.Keys}) etc.
     *
     * Note that this is usually not the best place for unregistering
     * listeners, etc., as listeners usually keep the child nodes
     * in memory, preventing them from being collected, thus preventing
     * this method to be called in the first place.

And that's what is happening in HTML5 project: listener created in NodeList.addNotify prevents NodeList subclass from being garbage collected and therefore removeNotify is never called.

Could you please clarify how this is intended to work? Thanks.
Comment 1 Milos Kleint 2013-06-10 06:18:09 UTC
Children.removeNotify() javadoc applies,  maybe it was rewritten at some stage to be more aligned with reality. It's been an issue with removeNotify() for a long time AFAIK.
Comment 2 Milos Kleint 2013-06-10 06:25:38 UTC
http://hg.netbeans.org/core-main/rev/54628ce756bb

our api is just a wrapper on top of nodes. their contract applies
Comment 3 David Konecny 2013-06-10 20:42:07 UTC
Thanks.

It's weird though as it makes removeNotify fairly useless. I had a look at several subclasses of NodeList and they all look the same: add listener in addNotify and remove it in removeNotify. Which is broken unless listener is a weak listener. But if listener is a weak one then you do not have to bother removing it in remoteNotify at all.
Comment 4 Milos Kleint 2013-06-11 04:21:28 UTC
(In reply to comment #3)
> Thanks.
> 
> It's weird though as it makes removeNotify fairly useless. I had a look at
> several subclasses of NodeList and they all look the same: add listener in
> addNotify and remove it in removeNotify. Which is broken unless listener is a
> weak listener. But if listener is a weak one then you do not have to bother
> removing it in remoteNotify at all.

indeed.  The behaviour is fairly unintuitive especially given it was named the same as JComponent.removeNotify().
The additional javadoc was added by pnejedly in 2007 - https://netbeans.org/bugzilla/show_bug.cgi?id=99895
Comment 5 Quality Engineering 2013-06-12 02:02:03 UTC
Integrated into 'main-golden', will be available in build *201306112301* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/54628ce756bb
User: Milos Kleint <mkleint@netbeans.org>
Log: #231005 updated javadoc