This discussion is archived
4 Replies Latest reply: Apr 5, 2010 9:36 AM by 843802 RSS

Cannot close TargetDataLine on OS X Snow Leopard

843802 Newbie
Currently Being Moderated
Hi,

I've written a program to record audio from the mic input and write the data to a file. The sound records and writes to the file as expected. The problem is I cannot stop the TargetDataLine by stopping or closing the line without the program crashing. The code runs fine on windows, but not on snow leopard.

If I call TargetDataLine.close() or TargetDataLine.stop(), the program crashes and I get the error:

java(1305,0x100501000) malloc: *** error for object 0xaa03: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Java Result: 134

This looks to be an os x bug.

The code runs in a thread. From outside the thread I call stop() and close() on the reference to the targetDataLine. If I just call stop it crashes, if I just call close it crashes, if I call stop and then close it crashes.
public void run(){
     System.out.println("Recording...");
     AudioFormat audioFormat = new AudioFormat(44100, 16, 1, true, false);
     try{
          targetDataLine = AudioSystem.getTargetDataLine(audioFormat);
          targetDataLine.open();
          targetDataLine.start();
          audioInputStream  = new AudioInputStream(targetDataLine);
          File outputFile = new File(outputFileName);
/* this line blocks and reads audio from the mic to the wave file until the targetDataLine is closed */
          AudioSystem.write(audioInputStream, Type.WAVE, outputFile);
     }
     catch(LineUnavailableException e){
     }
     catch(IOException e){
     }
     System.out.println("done recording");
}
I have also tried just closing the audioInputStream, which also results in the same crash and error.

Hopefully someone can shed some light on this.

Thanks!