I write a 3d model library.
Many different models, can share same OpenGL resources, as textures, buffers, programs, vertex-attribute-objects etc.
When a model is not needed anymore, I delete it. So, if OpenGL resources go unreferenced, must be deleted too, with OpenGL API commands.
So, what of the following is better to implement?
- Set OpenGL API command for delete OpenGL resources inside
finalize()
of object. I read in many articles thatfinalize()
is a devil. I believe it, because GC can be called at any time (after OpenGL context destruction), or not at all. - Implement reference-counting system for java classes which handle OpenGL resources. This needs lot of code and it is like reinvent the wheel, because GC already works with reference-counting.
So, what is the best approach in cases like that?