Regarding the issue causing Coppelia to crash due to simHandleVisionSensor and simGetVisionSensorImg

Typically: "How do I... ", "How can I... " questions
Post Reply
biscuit168
Posts: 6
Joined: 16 Nov 2023, 04:16

Regarding the issue causing Coppelia to crash due to simHandleVisionSensor and simGetVisionSensorImg

Post by biscuit168 »

Hello,
I encountered this issue while using a custom plugin. I created a thread within the plugin where I repeatedly call

Code: Select all

simHandleVisionSensor
and

Code: Select all

simGetVisionSensorImg
to retrieve images from the vision sensors. After running for a period of time, Coppelia crashes. My scene contains 10 vision sensors, and I retrieve images from each one in every iteration of the loop.

This is the exception reported by Visual Studio during the crash: Unhandled exception at 0x00007FFFEB977885 (ntdll.dll) in coppeliaSim.exe: 0xC0000374: The heap is corrupted. (Parameter: 0x00007FFFEBA2D0E0).

Here is my code snippet:

Code: Select all

while (!m_threadEnd)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(30));

    double* auxValues   = nullptr;
    int* auxValuesCount = nullptr;
    int ret             = simHandleVisionSensor(sim_handle_all, &auxValues, &auxValuesCount);
    if (ret >= 0)
    {
        simReleaseBuffer((char*)auxValues);
        simReleaseBuffer((char*)auxValuesCount);
        for (int i = 0; i < m_visionHandle.size(); i++)
        {
            unsigned char* img = simGetVisionSensorImg(m_visionHandle[i], 0, 0.0, nullptr, nullptr, resolution);
            if (img)
            {
                m_mediaServer->setFrame(m_visionUir[i], flippedImg, m_imageSize);
                simReleaseBuffer(img);
            }
            else
            {
                std::cout << "Failed to get image data from sensor " << i << std::endl;
            }
        }
    }
}
Please help me!
coppelia
Site Admin
Posts: 10794
Joined: 14 Dec 2012, 00:25

Re: Regarding the issue causing Coppelia to crash due to simHandleVisionSensor and simGetVisionSensorImg

Post by coppelia »

Hello,

please try simply calling simHandleVisionSensor like in following example:

Code: Select all

int ret = simHandleVisionSensor(sim_handle_all, nullptr, nullptr);
There should be no memory to release.

Cheers
biscuit168
Posts: 6
Joined: 16 Nov 2023, 04:16

Re: Regarding the issue causing Coppelia to crash due to simHandleVisionSensor and simGetVisionSensorImg

Post by biscuit168 »

coppelia wrote: 04 Nov 2025, 06:51 Hello,

please try simply calling simHandleVisionSensor like in following example:

Code: Select all

int ret = simHandleVisionSensor(sim_handle_all, nullptr, nullptr);
There should be no memory to release.

Cheers
Thank you very much for your reply. I tested it based on your suggestion, and it works!
biscuit168
Posts: 6
Joined: 16 Nov 2023, 04:16

Re: Regarding the issue causing Coppelia to crash due to simHandleVisionSensor and simGetVisionSensorImg

Post by biscuit168 »

coppelia wrote: 04 Nov 2025, 06:51 Hello,

please try simply calling simHandleVisionSensor like in following example:

Code: Select all

int ret = simHandleVisionSensor(sim_handle_all, nullptr, nullptr);
There should be no memory to release.

Cheers
In Debug mode, it runs very stably. When I tried testing in Release mode, Coppelia started crashing again. This is the exception reported by Visual Studio: An unhandled exception occurred at 0x00007FFFEB977885 (ntdll.dll) (in coppeliaSim.exe): 0xC0000374: The heap is corrupted. (Parameter: 0x00007FFFEBA2D0E0).
coppelia
Site Admin
Posts: 10794
Joined: 14 Dec 2012, 00:25

Re: Regarding the issue causing Coppelia to crash due to simHandleVisionSensor and simGetVisionSensorImg

Post by coppelia »

Can you reproduce the problem when using a script instead of a plugin? (i.e. calling sim.handleVisionSensor instead of simHandleVisionSensor)

sim.handleVisionSensor is basically implemented in a same way and should produce the same error. Here the sim.handleVisionSensor implementation:

Code: Select all

int _simHandleVisionSensor(luaWrap_lua_State* L)
{
    TRACE_LUA_API;
    LUA_START("sim.handleVisionSensor");

    int retVal = -1; // means error
    if (checkInputArguments(L, &errorString, lua_arg_integer, 0))
    {
        double* auxVals = nullptr;
        int* auxValsCount = nullptr;
        retVal = CALL_C_API(simHandleVisionSensor, luaToInt(L, 1), &auxVals, &auxValsCount);
        if (retVal != -1)
        {
            luaWrap_lua_pushinteger(L, retVal);
            int tableCount = 0;
            if (auxValsCount != nullptr)
            {
                tableCount = auxValsCount[0];
                int off = 0;
                for (int i = 0; i < tableCount; i++)
                {
                    pushDoubleTableOntoStack(L, auxValsCount[i + 1], auxVals + off);
                    off += auxValsCount[i + 1];
                }
                delete[] auxValsCount;
                delete[] auxVals;
            }
            for (int i = tableCount; i < 2; i++)
            {
                pushIntTableOntoStack(L, 0, nullptr); // return at least 2 aux packets, even empty
                tableCount++;
            }
            LUA_END(1 + tableCount);
        }
    }

    LUA_RAISE_ERROR_OR_YIELD_IF_NEEDED(); // we might never return from this!
    luaWrap_lua_pushinteger(L, retVal);
    LUA_END(1);
}
Cheers
biscuit168
Posts: 6
Joined: 16 Nov 2023, 04:16

Re: Regarding the issue causing Coppelia to crash due to simHandleVisionSensor and simGetVisionSensorImg

Post by biscuit168 »

coppelia wrote: 05 Nov 2025, 08:24 Can you reproduce the problem when using a script instead of a plugin? (i.e. calling sim.handleVisionSensor instead of simHandleVisionSensor)

sim.handleVisionSensor is basically implemented in a same way and should produce the same error. Here the sim.handleVisionSensor implementation:

Code: Select all

int _simHandleVisionSensor(luaWrap_lua_State* L)
{
    TRACE_LUA_API;
    LUA_START("sim.handleVisionSensor");

    int retVal = -1; // means error
    if (checkInputArguments(L, &errorString, lua_arg_integer, 0))
    {
        double* auxVals = nullptr;
        int* auxValsCount = nullptr;
        retVal = CALL_C_API(simHandleVisionSensor, luaToInt(L, 1), &auxVals, &auxValsCount);
        if (retVal != -1)
        {
            luaWrap_lua_pushinteger(L, retVal);
            int tableCount = 0;
            if (auxValsCount != nullptr)
            {
                tableCount = auxValsCount[0];
                int off = 0;
                for (int i = 0; i < tableCount; i++)
                {
                    pushDoubleTableOntoStack(L, auxValsCount[i + 1], auxVals + off);
                    off += auxValsCount[i + 1];
                }
                delete[] auxValsCount;
                delete[] auxVals;
            }
            for (int i = tableCount; i < 2; i++)
            {
                pushIntTableOntoStack(L, 0, nullptr); // return at least 2 aux packets, even empty
                tableCount++;
            }
            LUA_END(1 + tableCount);
        }
    }

    LUA_RAISE_ERROR_OR_YIELD_IF_NEEDED(); // we might never return from this!
    luaWrap_lua_pushinteger(L, retVal);
    LUA_END(1);
}
Cheers
I attempted to test using a script, but I found that sim.handleVisionSensor always returns a detectionCount of 0. This occurs regardless of whether I pass in sim.handle_all or a specific vision sensor handle as the parameter. Is this normal?
coppelia
Site Admin
Posts: 10794
Joined: 14 Dec 2012, 00:25

Re: Regarding the issue causing Coppelia to crash due to simHandleVisionSensor and simGetVisionSensorImg

Post by coppelia »

The first returned value should be exactly the same as for the C version of the same API function. If your vision sensors do not trigger a detection, then this is normal. By default, a vision sensor does not trigger a detection. To trigger a detection, attach a simulation script to the vision sensor with this code in it:

Code: Select all

function sysCall_vision(inData)
    outData = {}
    outData.trigger = true -- whether the sensor should trigger
    return outData
end
Then run the simulation and call sim.handleVisionSensor upon that sensor.

Cheers
Post Reply