I tried your example: when I click the Remove All Items button, I see several lines such as "newIndex: 0 is bigger or equal to size: 0" and finally one line "it was below zero". This is perfectly reasonable, because message delivery is
asynchronous* (see below for a more detailed explanation), and after you remove all items one by one, you check the size, and it is zero, but meanwhile you are still receiving events (about the changed index) from "the past".
Note that the latest version of the plugin has an additional parameter in insert and remove functions to suppress generation of events:
Code: Select all
simExtCustomUI_removeComboboxItem(int handle, int id, int index, bool suppressEvents = false)
This can be useful in such a case, where you don't want to be "disturbed" by this burst of events which are going to be obsolete in a fraction of a second; and is also needed when you want to change the content of the combobox inside the change callback to avoid an infinite loop.
* Long detailed explanation:
V-REP has at least two threads running, one for the user interface (UI), and one for the simulation (SIM). The lua scripts run into the SIM thread, while the user interface events are received in the UI thread, and any change in the UI must also be performed in the UI thread.
Executing an operation in a thread (UI) from another thread (SIM) is done via message exchange (using a built-in mechanism of Qt). Also the reporting of events (i.e. from UI to SIM) is done via message exchange using the same mechanism. Since message exchange is asynchronous, a message sent from one thread to another, can (hopefully) be delivered almost immediately, or in several milliseconds, or in several seconds. The only guarantee is that
eventually (i.e. sooner or later) will be delivered.
So when you remove all items from the combobox, several message are sent from the SIM thread to tell the UI thread to do that operation in Qt.
Immediately after you check the size of the combobox, and it is 0.
Meanwhile, a burst of messages are coming from the UI thread to the SIM thread containing the event notifications callbacks, which as said before, can be delayed in time.