2015-06-25 80 views
1

我正在編寫基於NetBeans平臺(7.4)的應用程序,並且已創建自定義的ProjecType和FileType。 ProjectType有一個弱文件更改偵聽器,用於刪除項目文件夾中的新文件。問題是,如果我打開一個項目,關閉它,然後再次嘗試重新打開它,它運行的內存,要麼拋出一個異常互斥:NetBeans平臺重新打開已關閉的項目StackOverflowError

塊引用java.lang.IllegalStateException:不應獲得Children.MUTEX同時保持ProjectManager.mutex

或IllegalStateException異常:

塊引用java.lang.IllegalStateException:在org.openide.nodes.Node.assignTo(Node.java:354) 在org.openide.nodes.EntrySupportDefault.justCo mputeNodes(EntrySupportDefault.java:212) at org.openide.nodes.ChildrenArray.nodes(ChildrenArray.java:88) at org.openide.nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:130) at org.openide。 nodes.EntrySupportDefault.getNodes(EntrySupportDefault.java:172) 在org.openide.nodes.Children.getNodes(Children.java:469)

另一個有趣的事情是,如果我關閉該項目並進行小更改爲項目文件夾,例如將其移動到新位置,或者甚至只是在該文件夾內打開文件,當我嘗試重新加載它時,它工作正常。 我已經嘗試完全刪除弱文件偵聽器,但沒有成功。而且我知道不是文件更改偵聽器問題,因爲Windows不會阻止我操作項目文件夾中的文件。 Bellow是我的ProjectNodeFactory,它爲項目文件夾中每個可識別的文件類型創建子節點。

public class MyProjectNodeFactory extends ChildFactory<FileObject> implements FileChangeListener { 

private final FileObject projectFolder; 

public MyProjectNodeFactory(MyProjectNode project) { 
    this.projectFolder = project.getProjectDirectory();     
    project.addFileChangeListener(this); 
    refresh(true); 
} 

@Override 
protected boolean createKeys(List<FileObject> list) { 

    for (FileObject fileObject : Collections.list(projectFolder.getData(false))) 
    { 
     if (fileObject.getExt().equals(Bundle.MY_FILETYPE_EXTENSION())) { 
      list.add(fileObject); 
     } 
    } 
    return true; 
} 

@Override 
protected Node[] createNodesForKey(FileObject key) { 
    ArrayList<Node> nodes = new ArrayList<Node>(); 
    // Add data files        
    try { 
     if(key.getExt().equals(Bundle.MY_FYLETYPE_EXTENSION())) 
     { 
      nodes.add(DataObject.find(key).getNodeDelegate());     
     } 
    } catch (DataObjectNotFoundException ex) { 
     Exceptions.printStackTrace(ex);     
    }        
    return (nodes.toArray(new Node[nodes.size()])); 
} 

另外我ProjecType LogicalView和ProjectNode

public class MyProjectType implements Project { 

private final FileObject projectDir; 
private final ProjectState state; 
private FileChangeListener fileChangeListener; 
private Lookup lkp; 

MyProjectType(FileObject dir, ProjectState state) { 
    this.projectDir = dir; 
    this.state = state; 
} 

@Override 
public FileObject getProjectDirectory() { 
    return projectDir; 
} 

@Override 
public Lookup getLookup() { 
    if (lkp == null) { 
     lkp = Lookups.fixed(new Object[]{ 
      // register your features here 
      this, 
      new MyProjectInfo(), 
      new MyProjectLogicalView(this),}); 
    } 
    return lkp; 
} 

public void addFileChangeListener(FileChangeListener regularFileChangeListener) { 
    this.fileChangeListener = (FileChangeListener)WeakListeners.create(FileChangeListener.class, regularFileChangeListener, this.projectDir.getPath());   
    this.projectDir.addFileChangeListener(fileChangeListener); 
} 

public void removeFileChangeListeners() { 
    if(fileChangeListener!=null) 
     this.projectDir.removeFileChangeListener(fileChangeListener); 
} 

private final class MyProjectInfo implements ProjectInformation { 

    @StaticResource() 
    public static final String PROJECT_ICON = "projectIcon.png"; 

    .................. 

} 

class MyProjectLogicalView implements LogicalViewProvider { 

    @StaticResource() 
    public static final String PROJECT_ICON = "projectIcon.png"; 

    private final MyProjectType project; 

    public MyProjectLogicalView(MyProjectType project) { 
     this.project = project;  
    } 

    @Override 
    public Node createLogicalView() { 
     try { 
      FileObject projectDirectory = project.getProjectDirectory(); 
      DataFolder projectFolder = DataFolder.findFolder(projectDirectory); 
      Node nodeOfProjectFolder = projectFolder.getNodeDelegate(); 

      return new ProjectNode(nodeOfProjectFolder, project); 
     } catch (DataObjectNotFoundException donfe) { 
      Exceptions.printStackTrace(donfe); 
      return new AbstractNode(Children.LEAF); 
     } 
    } 

    private final class ProjectNode extends FilterNode { 

     final MyProjectType project; 

     public ProjectNode(Node node, MyProjectType project) 
       throws DataObjectNotFoundException { 
      super(node, 
        Children.create(new MyProjectNodeFactory(project),true), 
        new ProxyLookup(
          new Lookup[]{ 
           Lookups.singleton(project), 
           node.getLookup() 
          })); 
      this.project = project; 
     } 

     @Override 
     public Action[] getActions(boolean arg0) { 
      return new Action[]{ 
       CommonProjectActions.newFileAction(), 
       CommonProjectActions.copyProjectAction(), 
       CommonProjectActions.deleteProjectAction(), 
       CommonProjectActions.closeProjectAction() 
      }; 
     } 

     ............... 
    } 

} 

回答

1

很多使用Google和調試的我是不是能夠檢測到爲什麼這種內存泄漏發生後,但我沒有找到在以下鏈接解決方案可能對遇到同樣問題的其他人有用。 Solution to Open/Close/Re-Open a NetBeans Platform ProjectType

解決方法是獲取子節點的副本,而不是僅僅委託。

public Node node(FileObject key) { 
    DataFolder folder = DataFolder.findFolder(key); 
    return folder.getNodeDelegate().cloneNode(); 
} 

我還是不明白爲什麼getNodeDelegate()是不夠的,但我相信,當項目首次關閉,一些引用不釋放,它重新打開時運行到的問題。通過獲取克隆,它創建了一個沒有這些問題的全新父節點。我希望這有助於某人,因爲我很難找到解決方案。

相關問題