Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 211906 Details for
Bug 373010
StyledCellLabelProvider leaks element from last cell even after table/tree is cleared
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
Example program
StyledCellLabelProviderLeaksElementOfLastMeasure.java (text/plain), 4.34 KB, created by
Eleanor Joslin
on 2012-03-01 13:57:10 EST
(
hide
)
Description:
Example program
Filename:
MIME Type:
Creator:
Eleanor Joslin
Created:
2012-03-01 13:57:10 EST
Size:
4.34 KB
patch
obsolete
>package com.corefiling.eclipse.ui.bugs; > >import java.lang.ref.WeakReference; > >import org.eclipse.jface.dialogs.MessageDialog; >import org.eclipse.jface.viewers.ArrayContentProvider; >import org.eclipse.jface.viewers.CellLabelProvider; >import org.eclipse.jface.viewers.StyledCellLabelProvider; >import org.eclipse.jface.viewers.TableViewer; >import org.eclipse.jface.viewers.TableViewerColumn; >import org.eclipse.jface.viewers.ViewerCell; >import org.eclipse.swt.SWT; >import org.eclipse.swt.events.SelectionAdapter; >import org.eclipse.swt.events.SelectionEvent; >import org.eclipse.swt.layout.GridData; >import org.eclipse.swt.layout.GridLayout; >import org.eclipse.swt.widgets.Button; >import org.eclipse.swt.widgets.Composite; >import org.eclipse.swt.widgets.Display; >import org.eclipse.swt.widgets.Shell; > > >/** > * Shows how a {@link StyledCellLabelProvider} leaks its elementOfLastMeasure field. > * > * Initially the table contains one element. When the "Remove Row" button is pressed, > * the table is emptied and refreshed, a {@link System#gc()} is performed, and the > * program checks whether it still holds a weak reference to the element, indicating > * that it has leaked. The {@link StyledCellLabelProvider} does leak the element. > * > * If you change {@link #USE_STYLED_CELL_LABEL_PROVIDER} to <code>false</code> and > * run the program again, it uses a plain {@link CellLabelProvider} instead and there > * is no leak. > */ >public class StyledCellLabelProviderLeaksElementOfLastMeasure { > > private static final boolean USE_STYLED_CELL_LABEL_PROVIDER = true; > > private WeakReference<?> _lastElementUpdated = new WeakReference<Object>(null); > > private Shell _shell; > private TableViewer _tableViewer; > > public void run() { > Display display = new Display(); > _shell = new Shell(display); > _shell.setLayout(new GridLayout(1, false)); > > Composite composite = new Composite(_shell, SWT.NONE); > composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); > composite.setLayout(new GridLayout(1, false)); > > _tableViewer = new TableViewer(composite, SWT.NONE); > GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true); > layoutData.heightHint = 400; > layoutData.widthHint = 600; > _tableViewer.getTable().setLayoutData(layoutData); > > TableViewerColumn column = new TableViewerColumn(_tableViewer, SWT.BORDER); > column.getColumn().setWidth(600); > column.setLabelProvider(createLabelProvider()); > _tableViewer.setContentProvider(new ArrayContentProvider()); > _tableViewer.setInput(new Object[] {new Object()}); > _tableViewer.refresh(); > > final Button button = new Button(composite, SWT.PUSH); > button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); > button.setText("Remove Row"); > button.addSelectionListener(new SelectionAdapter() { > @Override > public void widgetSelected(final SelectionEvent e) { > clearTableAndReportLeak(); > button.setEnabled(false); > } > }); > > _shell.pack(); > _shell.open(); > while (!_shell.isDisposed()) { > if (!display.readAndDispatch()) { > display.sleep(); > } > } > display.dispose(); > } > > private CellLabelProvider createLabelProvider() { > if (USE_STYLED_CELL_LABEL_PROVIDER) { > return new StyledCellLabelProvider() { > @Override > public void update(final ViewerCell cell) { > updateCellAndCacheElementWeakly(cell); > super.update(cell); > } > }; > } > else { > return new CellLabelProvider() { > @Override > public void update(final ViewerCell cell) { > updateCellAndCacheElementWeakly(cell); > } > }; > } > } > > private void updateCellAndCacheElementWeakly(final ViewerCell cell) { > cell.setText(cell.getElement().toString()); > _lastElementUpdated = new WeakReference<Object>(cell.getElement()); > } > > private void clearTableAndReportLeak() { > _tableViewer.setInput(new Object[0]); > System.gc(); > Object possiblyLeakedElement = _lastElementUpdated.get(); > if (possiblyLeakedElement != null) { > MessageDialog.openError(_shell, "Table Element Leaked", "This element was leaked: " + possiblyLeakedElement); > } > else { > MessageDialog.openInformation(_shell, "No Leak", "No element was leaked from the table."); > } > } > > public static void main(final String[] args) { > new StyledCellLabelProviderLeaksElementOfLastMeasure().run(); > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 373010
: 211906