开发者

Should I dispose jface CellEditors returned from EditorSupport

Should I dispose CellEditor returned from EditingSupport.getCellEditor, and if so when should I do it.

On one of the tutorials on jface TableViewer I saw following snippet:

public class FirstNameEditingSupport extends EditingSupport {

    @Override
    protected CellEditor getCellEditor(Object element) {
        return new TextCellEditor(viewer.getTable());
    }

so would this method leak memory if cell was edited multiple times? Or should I just lazy initialize CellEditor:

 public class FirstNameEditing开发者_运维百科Support extends EditingSupport {

    CellEditor editor;

    @Override
    protected CellEditor getCellEditor(Object element) {
        if(editor == null){
           editor = new TextCellEditor(viewer.getTable());
       }

       return editor;
    }


The first snippet looks like a nasty leak to me if you use such a EditingSupport for ViewerColumns. Just have a look at the source of ColumnViewerEditor, the class handling lots of the work around CellEditors. There are lots of lines like

if(part.getEditingSupport() != null)

(with part being a ViewerColumn). These calls create a Text instance in the constructor of the TextCellEditor. But since the EditingSupport instance is not assigned, it will be GCed immediately. The Text instance will not be disposed (until its parent is disposed). Leak.

So your second snippet seems way better.

Which tutorial? Maybe you should report that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜