Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Top 3 SWT Exceptions


Published on

Published in: Technology
  • Be the first to comment

Top 3 SWT Exceptions

  1. 1. Top 3 SWT Exceptions Lakshmi P Shanmugam Eclipse SWT Team IBM
  2. 2. Top 3 Exceptions <ul><li>Out of Handles Error </li></ul><ul><ul><li>no more OS handles for resources. </li></ul></ul><ul><li>Invalid Thread Access Exception </li></ul><ul><ul><li>accessing a widget from a non-UI thread </li></ul></ul><ul><li>Widget is disposed Exception </li></ul><ul><ul><li>accessing a widget after it is disposed </li></ul></ul>
  3. 3. Agenda <ul><li>Managing OS resources </li></ul><ul><li>Sleak tool </li></ul><ul><li>SWT Thread model </li></ul><ul><li>Introduction to UI thread </li></ul><ul><li>Programming in non-UI thread </li></ul>
  4. 4. Out of Handles Error when there are no more OS handles for the requested resources
  5. 5. Managing OS Resources <ul><li>OS resources created by </li></ul><ul><ul><li>SWT resource objects ( like Color , Cursor , Display , Font , GC , Image , Printer , Region </li></ul></ul><ul><ul><li>Widget. </li></ul></ul><ul><li>OS frees all of a program's resources when a program exits. But, OS resources are finite. </li></ul><ul><li>So, we need to free up resources when they are no longer needed, else our program can run out of resources. </li></ul><ul><li>Who has responsibility for disposing resources? Two simple rules to understand. </li></ul>
  6. 6. Rule #1 – If you created it, you dispose it (else don’t try to dispose it) <ul><li>SWT objects allocate any needed OS resources only in their constructor. </li></ul><ul><li>Font font = new Font (display,&quot;Courier&quot;,10, SWT.NORMAL); </li></ul><ul><li>font.dispose(); </li></ul>
  7. 7. <ul><li>If you didn't call the constructor, then you don't need to free the resources, so don't call dispose on the object </li></ul><ul><li>Font font = control.getFont (); </li></ul><ul><li>// do NOT call font.dispose() </li></ul><ul><li>addPaintListener(new PaintListener(){ </li></ul><ul><li>public void paintControl(PaintEvent event) { </li></ul><ul><li>event.gc.drawImage… </li></ul><ul><li>//do NOT call gc.dispose() </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul>
  8. 8. Rule #2 - Disposing the parent disposes the children <ul><li>Why? </li></ul><ul><ul><li>a widget cannot exist in the OS without a parent </li></ul></ul><ul><ul><li>avoids the burden of having to dispose each child of the parent </li></ul></ul><ul><li>A widget will never dispose a resource (font, color, etc) that you allocated </li></ul><ul><ul><li>E.g. Button.setImage(image); </li></ul></ul><ul><ul><li>they are not children of the widget </li></ul></ul><ul><ul><li>they could be shared by different widgets </li></ul></ul>
  9. 9. <ul><li>. final Shell shell = new Shell(display); </li></ul><ul><li>Composite composite = new Composite(shell); </li></ul><ul><li>Button button = new Button(composite, SWT.PUSH); </li></ul><ul><li>Font font = new Font (display, &quot;Courier&quot;, 10, SWT.NORMAL); </li></ul><ul><li>button.setFont(font); </li></ul><ul><li>button.addListener(SWT.Selection, Listener() { </li></ul><ul><li>public void handleEvent(Event e) { </li></ul><ul><li>shell.close(); //shell.dispose() </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>button.addDisposeListener( new DisposeListener(){ </li></ul><ul><li>  public void widgetDisposed(DisposeEvent e){ </li></ul><ul><li>      font.dispose(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  10. 10. SLeak <ul><li>a simple SWT tool that monitors SWT graphics resources </li></ul><ul><li>helps you detect resource leaks </li></ul>
  11. 11. Invalid Thread Access Exception when we access a widget from a non-UI thread
  12. 12. SWT thread model <ul><li>SWT implements a single-threaded UI model called apartment threading - only the UI-thread can invoke UI operations. </li></ul><ul><li>OS detects GUI events and places them in application event queues. </li></ul><ul><li>Application runs event loop in UI thread which reads & dispatches the GUI events from the queue. </li></ul><ul><ul><li>while (!shell.isDisposed ()) { </li></ul></ul><ul><ul><li>if (!display.readAndDispatch ()) { </li></ul></ul><ul><ul><li>display.sleep (); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  13. 13. SWT UI thread <ul><li>The thread in which the Display is created. All other widgets must be created in the UI thread. </li></ul><ul><li>It reads & dispatches events from OS queue and invokes listeners for these events. </li></ul><ul><li>Listener code is also executed in UI thread. This makes an SWT application generally quite responsive. </li></ul><ul><li>A long operation, when executed by a listener, will run in the UI-thread and prevent it from reading and dispatching events, thus hanging the application. </li></ul>
  14. 14. Programming in non-UI thread <ul><li>Long operations triggered by UI events should be performed in a separate thread to keep app GUI responsive to user. </li></ul><ul><li>SWT provides special access methods to allow other threads to perform operations on objects belonging to the UI-thread </li></ul><ul><ul><li>syncExec(Runnable r) </li></ul></ul><ul><ul><li>asyncExec(Runnable r) </li></ul></ul><ul><li>Applications that wish to call UI code from non-UI thread must provide a Runnable that contains the UI code. </li></ul><ul><li>UI thread will execute the Runnables during the event loop in the next reasonable opportunity. </li></ul>
  15. 15. <ul><li>asyncExec(Runnable) </li></ul><ul><ul><li>runnable is executed by UI-thread asynchronously. The thread which calls this method continues to run in parallel, and is not notified when the runnable has completed. </li></ul></ul><ul><li>syncExec(Runnable) </li></ul><ul><ul><li>runnable is executed by the UI-thread synchronously, causing the caller thread to wait for the runnable to finish. </li></ul></ul>
  16. 16. <ul><li>public static void main(String[] args) { </li></ul><ul><li>final Display display = new Display(); </li></ul><ul><li>Shell shell = new Shell(display); </li></ul><ul><li>final Text text = new Text(shell, SWT. MULTI ); </li></ul><ul><li>; </li></ul><ul><li>new Thread() { </li></ul><ul><li>public void run() { </li></ul><ul><li>//run long running operation here </li></ul><ul><li>//text.setText(“hello”); invalid thread access </li></ul><ul><li>if (display.isDisposed()) return ; </li></ul><ul><li>display.asyncExec( new Runnable() { </li></ul><ul><li> public void run() { </li></ul><ul><li>if (text.isDisposed()) return ; </li></ul><ul><li>text.setText( &quot;hello UI Thread&quot; ); </li></ul><ul><li> } </li></ul><ul><li>}); </li></ul><ul><li>System.out.println( “done” ); </li></ul><ul><li> } </li></ul><ul><li>}.start(); </li></ul>
  17. 17. Widget is Disposed when we try to access a widget which is already disposed
  18. 18. Widget disposed ? <ul><li>It is necessary to check if your widget is disposed from within the runnable when using asyncExec or syncExec. </li></ul><ul><li>At the time the runnable is invoked the widget may have been disposed. We can’t be sure about the state of the widget when runnable executes. </li></ul>
  19. 19. <ul><li>Management of OS Resources </li></ul><ul><li>SWT Home Page </li></ul><ul><ul><li> </li></ul></ul><ul><li>Sleak </li></ul><ul><li> </li></ul>References
  20. 20. Thank you 