Closed cxbrooks closed 8 years ago
I submitted a pull request #58, below is the most important part of change:
diff --git a/src/main/c/src/SerialImp.c b/src/main/c/src/SerialImp.c
index fdd1c9b..cce8fd1 100644
--- a/src/main/c/src/SerialImp.c
+++ b/src/main/c/src/SerialImp.c
@@ -3074,7 +3074,13 @@ int read_byte_array( JNIEnv *env,
now = GetTickCount();
if ( now-start >= timeout )
{
- eis->eventflags[SPE_DATA_AVAILABLE] = flag;
+ struct event_info_struct *eis2 = ( struct event_info_struct * )
+ get_java_var_long( env, *jobj,"eis","J" );
+ if (eis2 == NULL) {
+ report("read_byte_array(): eis was null, this can happen if reading after RXTXPort.run() returns while reading.");
+ } else {
+ eis->eventflags[SPE_DATA_AVAILABLE] = flag;
+ }
return bytes;
}
}
madhephaestus suggested that I make an issue for this and add a comment or commit.
Under Mac OS X, running a Ptolemy model (SerialHelloWorld.xml )model was crashing Java in readArray(). I don't have a small test case for this, and my changes are more of a workaround than a fix.
The model opens two ports, writes to one port and halts after 5 seconds.
The code does not use the RXTXListener facility, instead it creates reader and writer threads.
The cause is that when the Java RXTXPort.run() method exits, a C struct named eis goes out of scope. If the drain thread tries to access eis after a timeout, then a segfault occurs.
What's happening is that in nrjavaserial/src/main/c/src/SerialImp.c, readArray() is calling read_byte_array() and read_byte_array() is crashing when eis is accessed:
The reason that it is crashing here is because eis is allocated in eventLoop() in SerialImp.c by struct event_info_struct eis:
init_threads() sets the Java eis object:
nrjavaserial/src/main/java/gnu/io/RXTXPort.java invokes eventLoop()
So, if the run() method ends, the eis automatic variable in eventloop() goes out of scope.