CustomUI combobox onChange event called with invalid argumen

Report crashes, strange behaviour, or apparent bugs
Post Reply
tmalheiro
Posts: 29
Joined: 27 Nov 2015, 22:53

CustomUI combobox onChange event called with invalid argumen

Post by tmalheiro »

Dear coppelia,

Sometimes, I am having a strange behavior with the CustomUI combobox onChange event.
This event is being called, e.g. with the newIndex being 0 and the number of items in combobox being also 0!
This is erradic, sometimes it happens, other times it does not. Other times the newIndex is e.g. 2....

I noticed it happens when I try to delete items from the combobox in bulk.
I explain below what I do:

On the XML I do, e.g.:

Code: Select all

<combobox id="1005" onchange="comboboxChange">
                    <item>First item</item>
                    <item>Second item</item>
                    <item>Third item</item>
                </combobox>
while in the sim_childscriptcall_initialization.

On actuation, I need to clear all the current items and update with new ones.
Since there is no remove all, I do:

Code: Select all

--remove all current items
        for i=1, simExtCustomUI_getComboboxItemCount(ui, 1005) do
            simExtCustomUI_removeComboboxItem(ui, 1005, 0)
        end
Best,
Tiago

fferri
Posts: 1193
Joined: 09 Sep 2013, 19:28

Re: CustomUI combobox onChange event called with invalid arg

Post by fferri »

The selected index of a combobox in Qt can change when removing items.

Suppose you have your combobox:

Code: Select all

    <combobox id="1005" onchange="comboboxChange">
        <item>First item</item>
        <item>Second item</item>
        <item>Third item</item>
    </combobox>
and "Third item" is selected (selected index is 2).

Now if you remove "First item" (index 0), item "Third item" will remain selected, but its index will now be 1, and you will receive an onchange event with new index equal to 1.

When you remove the last item, the selected index will change to -1.

Then, when you add an item to an empty combobox, the selected index will change again to 0.
Last edited by fferri on 08 Jul 2016, 18:53, edited 2 times in total.

tmalheiro
Posts: 29
Joined: 27 Nov 2015, 22:53

Re: CustomUI combobox onChange event called with invalid arg

Post by tmalheiro »

Dear fferri,

Thank you for your help.

I understand what you mean, and I do expect that.

The problem is different. I am receiving such event with selected index being 0, or 2 (and others) while simExtCustomUI_getComboboxItemCount returns 0.
As previously described, under the same conditions (as far as possible) this behavior is not always expressing itself.

I will try to build a minimum working example to evaluate and upload it here for further analysis.

Edit:
I have put together a working example where the problem appears. I can send it to you if you will.

I am sorry, I noticed your update in the reply. But I do not see that always happening. Sometimes what you described happens, and others it does not.

Best,
Tiago

fferri
Posts: 1193
Joined: 09 Sep 2013, 19:28

Re: CustomUI combobox onChange event called with invalid arg

Post by fferri »

If you have a minimal testcase showing the issue please do send it

tmalheiro
Posts: 29
Joined: 27 Nov 2015, 22:53

Re: CustomUI combobox onChange event called with invalid arg

Post by tmalheiro »

yes, you can find it here https://drive.google.com/file/d/0B_Iig0 ... sp=sharing.

The steps I take which more reliably leads to error:
Run simulation, in combobox tab select one of the last element, click Remove All.

when error occurs, a message appears in status bar.

Edit: Higher computational load usually also leads to more errors (or it seemed).

Best,
Tiago

fferri
Posts: 1193
Joined: 09 Sep 2013, 19:28

Re: CustomUI combobox onChange event called with invalid arg

Post by fferri »

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.

tmalheiro
Posts: 29
Joined: 27 Nov 2015, 22:53

Re: CustomUI combobox onChange event called with invalid arg

Post by tmalheiro »

Dear fferri,

Thank you for the explanation.
I totally understand what you describe.
Since from VREP documentation embedded scripts are always synchronous (or at least it seemed to me until now), I was not expecting a call from a script to enqueue a request which could be later on processed.

Thank you for you time,
Best,
Tiago

coppelia
Site Admin
Posts: 10336
Joined: 14 Dec 2012, 00:25

Re: CustomUI combobox onChange event called with invalid arg

Post by coppelia »

Hello Tiago,

yes, that is correct, the embedded scripts execute in a synchronous fashion, but with that specific plugin, the script needs to interact with the UI (i.e. Qt), which needs to run in another thread.

Cheers

Post Reply