garbage collection of graphics objects
ddyer0 opened this issue · comments
I've had a long running, occasional bug where a background that was drawn into an image
is damaged; basically incompletely drawn. I had an epiphany last night; that the underlying
cause is that the java-level pointer to system object is garbage collected while the drawing
is still in process. I found this:
protected void finalize() {
if (nativeGraphics != null) {
impl.disposeGraphics(nativeGraphics);
}
}
A simple experiment to retain the objects returned by getGraphics() seems to have fixed the
problem. I have a pretty disciplined use strategy for getGraphics() so this is satisfactory for
me; but disposeGraphics ought to use some strategy to delay disposal until drawing is complete.
If the finalizer is invoked it means there was a GC process that already collected the object. That is WAY WAY WAY too late.
I don't see any valid use case allowed by our API that would allow it.
my actual use case is i create a writable image, get a graphics, perform hundreds of graphics operations, and the move on, at which point the graphics becomes garage and collectable. later, i expect all the operations i've performed to be reflected in the image.
what is actually happening is if a Gc collects the graphics before the underlying operations have finished,
the image i am building is incomplete.
The number of operations doesn't matter. If you can create a case where an object is reachable but still gets GCd that would be an interesting test case. Either way, this line isn't the bug.
the GC is legitimate, but the system level operations are still continuing, and the "destroy" operation
triggered by the finalize is terminating them before they complete.