Segmentation fault while trying to reconnect to the camera stream

I’m trying to create software that reconnects to the camera when a problem occurs. After some reconnection attempts, I face the following error:

[New Thread 0x7fffd5e00000 (LWP 359929)]
[New Thread 0x7fffd5400000 (LWP 359930)]
[Streaming] Error: Stream Opening returned TIMEOUT (350)
FAILURE. The function does not perform as expected.
[2025-03-05 18:19:13 UTC][ZED][WARNING] FAILURE in sl::ERROR_CODE sl::Camera::open(sl::InitParameters)
[Streaming] Metadata timeout. the size is equal to -1 instead of 21960. Skipping.
[Thread 0x7fffd5400000 (LWP 359930) exited]
[Thread 0x7fffd5e00000 (LWP 359929) exited]
[2025-03-05 18:19:18 UTC][ZED][INFO] Logging level INFO
[2025-03-05 18:19:18 UTC][ZED][ERROR] [ZED] sl::Camera::Open has not been called, no Camera instance running.
[2025-03-05 18:19:18 UTC][ZED][ERROR] [ZED] sl::Camera::Open has not been called, no Camera instance running.
[Streaming] Warning: Failed to retrieve camera settings from sender
[New Thread 0x7fffd5400000 (LWP 360063)]
[New Thread 0x7fffd5e00000 (LWP 360064)]
[Streaming] Error: Stream Opening returned TIMEOUT (350)
FAILURE. The function does not perform as expected.
[2025-03-05 18:19:29 UTC][ZED][WARNING] FAILURE in sl::ERROR_CODE sl::Camera::open(sl::InitParameters)
[Streaming] Metadata timeout. the size is equal to -1 instead of 21960. Skipping.
[Thread 0x7fffd5e00000 (LWP 360064) exited]

Thread 44 "zed_reconnect_t" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffd5400000 (LWP 360063)]
0x00007fffece1ad20 in main_arena () from /lib/x86_64-linux-gnu/libc.so.6
(gdb) 

I made a minimal code to test if I was doing something wrong:

#include <sl/Camera.hpp>

int main(){

    sl::Camera zed;
    sl::InitParameters initParams;
    initParams.input.setFromStream("192.168.1.15", 30000);
    sl::ERROR_CODE cameraStatus;

    while (true) {
        if (!zed.isOpened()) {
            cameraStatus = zed.open(initParams);
            if (cameraStatus != sl::ERROR_CODE::SUCCESS) {
                std::cout << cameraStatus << ". " << sl::toVerbose(cameraStatus) << "\n";
                std::this_thread::sleep_for(std::chrono::seconds(5));
                continue;
            }
        }

        std::cout << "Camera initialized!\n";

        while (true) {
            cameraStatus = zed.grab();
            if (cameraStatus != sl::ERROR_CODE::SUCCESS) {
                std::cout << cameraStatus << ". " << sl::toVerbose(cameraStatus) << "\n";
                zed.close();
                std::this_thread::sleep_for(std::chrono::seconds(1));
                break;
            }
        }
    }
    return 0;
}

I’m using the SDK version 4.2.5.

Any suggestion?

Hi @Mat198
you do not need this type of code because the camera reconnection is automatically handled by the ZED SDK.

I recommend to set async_grab_camera_recovery to true and check for the grab function to return sl::ERROR_CODE::CAMERA_REBOOTING when a disconnection recover is ongoing.

I advise you to check how this process is handled in the ROS 2 Wrapper as a valid example:

I knew I was doing something wrong. Thank you!