0

I am painting images of a Canvas with a GC (of course) but after I had repainted a new frame with new imaged I need to delete the previous but I don't know how.

I want to dispose the data on the canvas and then redraw the frame but I don't know how. Anyone knows how can I dispose the data on the canvas without disposing the canvas itself?

  • For the memory leak - if you created the GC, did you dispose of it with `GC.dispose()`? For clearing the canvas, have you tried `Canvas.clearArea()`? – Andy Thomas Jan 12 '16 at 23:25
  • I have tried to dispose the GC it aint working. – Assaf Karavani Jan 12 '16 at 23:28
  • And there is no such method clearArea() – Assaf Karavani Jan 12 '16 at 23:28
  • You don't have to dispose of the drawings you make on the Canvas. Your drawing operations are setting pixels in an image, not creating disposable objects. When you say that `GC.dispose()` is not working, what is it not doing that you expect? Sorry, not sure where I pulled `clearArea()` from. You might be interested in `Canvas.drawBackground()`, to paint over your prior drawing. If you're properly disposing the GC if required, your memory leak may be coming from elsewhere. A Java or native heap debugger may help. – Andy Thomas Jan 13 '16 at 01:43
  • @AssafKaravani What makes you think that you have a memory leak at all? – Baz Jan 13 '16 at 09:00
  • Because the program works fine and after several redraws it crashs with the "no more handles" error – Assaf Karavani Jan 13 '16 at 09:15
  • @AssafKaravani That's why you should post the relevant code and the stacktrace (if any). – Baz Jan 13 '16 at 09:18
  • 2
    @AssafKaravani - That message tells you that you have too many handles, which may indicate that you have a handle leak. It does not tell you that you have a memory leak. For example, this might be caused by failing to dispose of Fonts, Colors, Images or GCs. More background is provided by the prior question ["SWT No More Handles"](http://stackoverflow.com/questions/2018553/swt-no-more-handles). – Andy Thomas Jan 13 '16 at 16:17

2 Answers2

0

You don't have to dispose of the old data on the Canvas, just use GC.fillRectangle to fill the canvas with the background color in your paint listener.

Something like:

@Override
public void paint(final PaintEvent event)
{
  Rectangle clientArea = getClientArea();

  GC gc = event.gc;

  gc.setBackground(getBackground());

  gc.fillRectangle(clientArea);

  ... draw your new data
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
0

I have fixed it guys, thank you all! The problem was that I was creating new Images but wasn't disposing them.