Yes it's not very elegant, but it's by far the most reliable way. Java tries to be a hammer for every nail, so there is this thing called finalizer - an Object method called upon garbage collecting your instance. So it ideally does exactly what you need but practically better be avoided. If you read the related Javadoc carefully, you'll find out that:
- Your precious renderer deleting code will be called not when you or your code thinks it's appropriate, but when the reference counting engine decides. And even worse, not at that specific point, but at any time later.
- You can't know and you can't influence, which thread will call the finalizer. A double barrel shot in the foot if the code is related to UI operations (i understand that your code is).
Though, you can use finalizer for a safety check.
- wrap the renderer lifecycle (create/use/delete) in an object with public method for deleting
- upon explicit deleting, set an internal flag
- override
finalize()
and check the flag state there. If not marked as deleted, log nasty error