[sdljava-users] JNI pitfall
Status: Beta
Brought to you by:
ivan_ganza
From: Robert S. <the...@gm...> - 2005-03-03 19:55:35
|
Hi list, I was writing on a small demo app which at some place included these lines: if (!measureFPS) { SDLMixer.openAudio(44100, SDLMixer.AUDIO_S16SYS, 2, 2048); System.out.println("Loading song."); MixMusic song = SDLMixer.loadMUS(Thread.currentThread() .getContextClassLoader() .getResourceAsStream("memesong.ogg")); System.out.println("Playing song: " + song); SDLMixer.playMusic(song, 0); } The code does not look very special but at runtime something odd will happen: You will hear a small bit of music and then it stops. The reason for this is that the MixMusic instance 'song' gets garbage collected when the if-block ends. I describe this here because this behavior is caused by the way Swig handles native pointers and can be a pitfall for others as it was for me. You can work around this problem by assigning an instance like MixMusic (which in the above example is a local variable) to an instance or class variable. @Ivan: If you are going to write a FAQ for SDLJava sometime it would be good to add this issue to it. cu Robert |