JVM crashes when calling native JOGL method in finalize()

I’m using jogl-1.1.1-windows-i586 version of the JOGL library. When I call glDeleteLists() from a finalize method of a class, the JVM crashes and displays the following error message:

An unexpected error has been detected by Java Runtime Environment:

EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x5f1935a6, pid=2360, tid=4680

Java VM: Java HotSpot™ Client VM (10.0-b23 mixed mode, sharing windows-x86)

Problematic frame:

C [OPENGL32.dll+0x35a6]

When I try to debug my application under Eclipse, an error message is thrown instead of entering the finalize function. The error message says:
‘process model delta’ has encountered a problem

I would really appreciate any about clues what can be the problem.

Bye,
Tamas

The GL context you’re won’t be properly active at the time that you call it. In the docs, it’s advised not to keep a reference to a GL object in your classes, but instead basically always passing them around via the GLEventListener callbacks. Don’t take my word for that, but I’m afraid I don’t have time to verify that more accurately right now.

I don’t know what the best practice for cleaning up resources is - probably quite dependent on the architecture of your program. I’d appreciate any comments others might have on this; to be honest I’ve been rather putting off dealing with it, although I think I’ve a fair idea of what I’ll eventually end up doing (if I eventually end up doing substantially more with JOGL, which isn’t a forgone conclusion). It’s not my most pressing need at the moment.

Obviously the problem is, that I can’t pass the refence of the GL context to the finalize() method. On the other hand, I’m pretty sure, that the context I use is up to date.

Thanks for your reply!

Indeed; you need to have control of your resources outside the context of normal java garbage collection… would appreciate comments from others.

[quote]On the other hand, I’m pretty sure, that the context I use is up to date.
[/quote]
I don’t think you can be sure of any such thing when garbage collection is called; not sure of all the ins and outs, but the GL context needs to be activated on the current thread - garbage collection happens on a totally different, system controlled, thread, to any on which the GL could’ve been used.

JOGL deletes your display lists when the JVM shuts down. Do you need to delete them before?

Yes I have to delete them before the JVM shuts down, since they are not used any more, but occupying a huge amount of memory. Of course there is a workaround, and I can manually delete them, but I have the feeling that using the finalize() method would be a cleaner and more elegant approach.

I only want to add that finalize methods change the way how the object is treated by the GC and may cause performance problems. It is also often seen as an antipattern.

you’re right. freeing resources is for sissies. :stuck_out_tongue:

Use a “dirtier” approach. Overriding finalize() when using JOGL gave me strange results.