문제

I have a small problem.

My Java application holds some native resources. I want to free those resources just before I exit the application. Therefore I wrote a WindowListener that frees those resources in windowClosed event.

The problem is that I am having a JFrame with EXIT_ON_CLOSE, so when I close the window, it shuts down the JVM and the event will never reach me.

If I use windowClosing event, then somebody can actually override the closing event ater I freed the resources. This will then lead to segfaults in native code.

I can just put away the EXIT_ON_CLOSE and call System.exit(0) from the listener, but that does not seem clean to me.

도움이 되었습니까?

해결책

You could maybe add a shutdown hook and free the native resources there? Runtime.getRuntime().addShutdownHook(Thread t). See more info here.

다른 팁

You can hide the JFrame in the windowClosing before freeing the resources so you can make sure it doesn't get interrupted

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top