Win32 JNI crash in javax.comm(small example included)

Øyvind Harboe oyvind.harboe@zylin.com
Wed May 21 18:27:00 GMT 2003


I'm struggling with access violations in my serial port 
application, and I have been able to pretty much narrow it down. 
Works fine in the JRE.

3.2 runs for a bit longer, but thats probably just a red herring.

Output from debug session and example program included below.


To run the app:

- compile 

\thisiscool-gcc\gcc-3.3\bin\gcj --main=TestSerial comm.jar -fjni -o
test2.exe TestSerial.java

- copy win32com.dll into same directory as test2.exe (get comm.jar
+ win32com.dll from http://www.harboe.org/oyvind/rootsets.zip), or 
java.sun.com




>From 3.2 compiled app:

...
Test 137
warning: HEAP[test2.exe]:
warning: HEAP: Free Heap block d89d40 modified at d89d74 after it was
freed


Program received signal SIGTRAP, Trace/breakpoint trap.
0x77f9180d in ?? ()
(gdb) thread apply all bt

Thread 2 (thread 3176.0x96c):
#0  0x77f88e63 in ?? ()
#1  0x77ea9c13 in WaitForMultipleObjects () from
/ecos-C/WINNT/system32/KERNEL32
.DLL
#2  0x004248e1 in _ZN4java4lang6Object4waitExi ()
#3  0x00414e64 in _ZN4java4lang6Object4waitEv ()
#4  0x00434409 in _ZN3gnu3gcj7runtime15FinalizerThread3runEv ()
#5  0x004250a9 in _Z13_Jv_ThreadRunPN4java4lang6ThreadE ()
#6  0x0043795d in _Z12really_startPv@4 ()
#7  0x00481dc5 in thread_start@4 ()
#8  0x77e8b2d8 in lstrcmpiW () from /ecos-C/WINNT/system32/KERNEL32.DLL

Thread 1 (thread 3176.0x860):
#0  0x77f9180d in ?? ()
#1  0x77f9bdaa in ?? ()
#2  0x77fb001a in ?? ()
#3  0x77f9bbf2 in ?? ()
#4  0x77fc953e in ?? ()
#5  0x780034d8 in calloc () from /ecos-C/WINNT/system32/msvcrt.dll
#6  0x10001657 in
win32com!_Java_com_sun_comm_Win32SerialPort_nativeConstructor@
12 ()


>From 3.3 compiled app:



C:\eclipse\workspace\scripts\native>\cygwin\bin\gdb test2
GNU gdb 2003-03-03-cvs (cygwin-special)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you
are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for
details.
This GDB was configured as "i686-pc-cygwin"...
(gdb) run
Starting program: /ecos-C/eclipse/workspace/scripts/native/test2.exe
...
[stuff deleted]
...
Test 29
warning: HEAP[test2.exe]:
warning: HEAP: Free Heap block 68a030 modified at 68a05c after it was
freed


Program received signal SIGTRAP, Trace/breakpoint trap.
0x77f9180d in ?? ()
(gdb) thread apply all bt

Thread 2 (thread 2408.0x858):
#0  0x77f88e63 in ?? ()
#1  0x77ea9c13 in WaitForMultipleObjects () from
/ecos-C/WINNT/system32/KERNEL32
.DLL
#2  0x00423c08 in _ZN4java4lang6Object4waitExi ()
#3  0x00411164 in _ZN4java4lang6Object4waitEv ()
#4  0x0043889e in _ZN3gnu3gcj7runtime15FinalizerThread3runEv ()
#5  0x00424707 in _Z13_Jv_ThreadRunPN4java4lang6ThreadE ()
#6  0x0043bf37 in _Z12really_startPv@4 ()
#7  0x0047feb8 in thread_start@4 ()
#8  0x77e8b2d8 in lstrcmpiW () from /ecos-C/WINNT/system32/KERNEL32.DLL

Thread 1 (thread 2408.0xc68):
#0  0x77f9180d in ?? ()
#1  0x77f9c5d1 in ?? ()
#2  0x77fb001a in ?? ()
#3  0x77f9bbf2 in ?? ()
#4  0x77fc953e in ?? ()
#5  0x780010a8 in malloc () from /ecos-C/WINNT/system32/msvcrt.dll
#6  0x78001045 in malloc () from /ecos-C/WINNT/system32/msvcrt.dll
#7  0x0042a765 in
_Z25_Jv_JNI_GetStringUTFCharsP10_Jv_JNIEnvPN4java4lang6StringE
Pb@12 ()
#8  0x1000167b in
win32com!_Java_com_sun_comm_Win32SerialPort_nativeConstructor@
12 ()
(gdb)




import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.NoSuchPortException;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class TestSerial
{

	private InputStream inputStream;

	private OutputStream outputStream;

	private String foobar;

	public void run()
	{
		try
		{
			SerialPort sPort =
				(SerialPort)
CommPortIdentifier.getPortIdentifier("COM2").open(
					"transfer",
					0);

			try
			{
				sPort.setSerialPortParams(
					57600,
					SerialPort.DATABITS_8,
					SerialPort.STOPBITS_1,
					SerialPort.PARITY_NONE);

	
sPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);

				inputStream = sPort.getInputStream();
				outputStream = sPort.getOutputStream();

				sPort.notifyOnDataAvailable(true);

				/* create an anonymous class to handle
this event. */
				SerialPortEventListener t;
				t = new SerialPortEventListener()
				{
					public void
serialEvent(SerialPortEvent event)
					{
						if (event.getEventType()
							==
SerialPortEvent.DATA_AVAILABLE)
						{
							/* We got more
data, wake up ourselves. */
							//
gotMore();
						}
					}
				};

				sPort.addEventListener(t);
				
				

				outputStream.write(new byte[1]);
				
				sPort.removeEventListener();
				
			} catch (TooManyListenersException e)
			{
				e.printStackTrace();
			} finally
			{
				sPort.notifyOnDataAvailable(false);
				sPort.close();
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (PortInUseException e)
		{
			e.printStackTrace();
		} catch (UnsupportedCommOperationException e)
		{
			e.printStackTrace();
		} catch (NoSuchPortException e)
		{
			e.printStackTrace();
		}
	}

	public static void main(String[] args)
	{
		// avoid dependency on the silly javax.comm.properties
file
		// works for Java Web Start *and* GCJ.
		String driverName = "com.sun.comm.Win32Driver";
		try
		{
			javax.comm.CommDriver commDriver =
				(javax.comm.CommDriver)
Class.forName(driverName).newInstance();
			commDriver.initialize();
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		} catch (InstantiationException e)
		{
			e.printStackTrace();
		} catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		for (int i = 0; i < 1000; i++)
		{
			System.out.println("Test " + i);
			new TestSerial().run();
		}
	}
}



More information about the Java mailing list