我应该处理从 EditorSupport 返回的 jface CellEditors
我是否应该处置从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您为
ViewerColumns
使用这样的EditingSupport
,第一个片段对我来说看起来像是一个令人讨厌的泄漏。只需查看 ColumnViewerEditor,处理大量工作的类单元格编辑器
。有很多类似的行(
part
是ViewerColumn
)。这些调用在TextCellEditor
的构造函数中创建一个Text
实例。但由于EditingSupport
实例未分配,因此它会立即被GC。Text
实例不会被释放(直到它的父实例被释放)。泄露。所以你的第二个片段似乎更好。
哪个教程?也许你应该报告这一点。
The first snippet looks like a nasty leak to me if you use such a
EditingSupport
forViewerColumns
. Just have a look at the source of ColumnViewerEditor, the class handling lots of the work aroundCellEditors
. There are lots of lines like(with
part
being aViewerColumn
). These calls create aText
instance in the constructor of theTextCellEditor
. But since theEditingSupport
instance is not assigned, it will be GCed immediately. TheText
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.