B4J Question jSerial crash when device is removed

MM2forever

Active Member
Licensed User
Longtime User
Hello folks,

I have an issue with B4J running on a Raspberry Pi.

I use jSerial und jRandomFileAccess to open serial connections to devices plugged intom the Raspberry via USB. The devices are basically USB-Serial converters.

When I remove a device without properly closing the connection first I get this error and the program completely crashes:

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

Its different I guess when you remove the whole system device instead of just removing a serial cable in the classic way.
So, how can I catch the error and "calmly" unregister that port without the whole thing crashing?
I tried a couple of things like using

Sub Application_Error

but it doesnt really catch the error or let me return to a working state.

Thanks for your help,
Christian
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes, it has no java-style error message, that worries me too.
For a good reason. The actual serial implementation is done in native code (C/C++). Hard crashes in native code cannot be trapped as they cause the JVM to crash.

What you can do, is to create another app that manages the serial app. You can use jShell for that. If the serial app terminates then wait a few seconds and start it again.
 
Upvote 0

MM2forever

Active Member
Licensed User
Longtime User
Ohoh.

Thats a really bad workaround option because the app also serves a webserver with websockets...

Maybe I can start a communication app for every port I want to connect and somehow pipe it to the "main" -app so i just restart those small ones handling communication.

EDIT:

Opps Erel Im sorry thats kind of what you said I guess, i just didnt get it the first time
 
Upvote 0
Top