我应该处理从 EditorSupport 返回的 jface CellEditors

发布于 2024-11-07 12:08:37 字数 749 浏览 5 评论 0原文

我是否应该处置从 EditingSupport.getCellEditor 返回的 CellEditor,如果是,我应该何时处置。

在 jface TableViewer 的教程之一上,我看到了以下片段:

public class FirstNameEditingSupport extends EditingSupport {

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

如果多次编辑单元格,此方法会泄漏内存吗?或者我应该延迟初始化CellEditor

 public class FirstNameEditingSupport extends EditingSupport {

    CellEditor editor;

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

       return editor;
    }

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 FirstNameEditingSupport extends EditingSupport {

    CellEditor editor;

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

       return editor;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪痕残 2024-11-14 12:08:37

如果您为 ViewerColumns 使用这样的 EditingSupport,第一个片段对我来说看起来像是一个令人讨厌的泄漏。只需查看 ColumnViewerEditor,处理大量工作的类单元格编辑器。有很多类似的行

if(part.getEditingSupport() != null)

partViewerColumn)。这些调用在 TextCellEditor 的构造函数中创建一个 Text 实例。但由于 EditingSupport 实例未分配,因此它会立即被GC。 Text 实例不会被释放(直到它的父实例被释放)。泄露。

所以你的第二个片段似乎更好。

哪个教程?也许你应该报告这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文