NvBufferCreateEx failes in thread

Hello,

I want to use buffer streams with my ligargus application. However, I run into problem while creating the DMA buffers and EGLImages.

I use this code to create the buffers:

 for (size_t i = 0; i < NUM_BUFFERS; i++)
    {
        NvBufferCreateParams params = {0};

        params.width = width;
        params.height = height;
        params.layout = NvBufferLayout_Pitch;
        params.colorFormat = NvBufferColorFormat_NV12;
        params.payloadType = NvBufferPayload_SurfArray;
        params.nvbuf_tag = NvBufferTag_CAMERA;

int ret = NvBufferCreateEx(&m_dmaBuffer[i], &params);
        if (ret)
        {
            ERR("Failed to allocate NativeBuffer '%d'", port);
            return false;
        }
        m_image[i] = NvEGLImageFromFd(EGL_NO_DISPLAY, m_dmaBuffer[i]);
        if (m_image[i] == EGL_NO_IMAGE_KHR)
        {
            ERR("Failed to allocate EGLImage '%d'", port);
            return false;
        }
    }

which works fine if I use it in my main thread, but if I put the same code in a sub thread I get a Segmentation fault (core dumped) with the NvBufferCreateEx(&m_dmaBuffer[i], &params); call.

Sometimes the buffer creation succeeds, but then I get a cannot set thread specific data error with the NvEGLImageFromFd(EGL_NO_DISPLAY, m_dmaBuffer[i]);call.

Do I have to perform some extra sync operation when creating nvbuffes in a thread?

Hi,
Please share a test code so that we can build/run it to replicate the issue. And please also share your Jetpack version.

I’m using Jetpack version 4.6.2

This is a minimal example to recreate the failure get:
main.cpp (1.7 KB)

Hi,
Please also share the make command for building the sample. Thanks.

I use a cmake enviroment to build the application:
CMakeLists.txt (3.7 KB)

The generated make command is:
/usr/local/cuda/bin/nvcc -I/usr/src/jetson_multimedia_api/include -I/usr/src/jetson_multimedia_api/include/libjpeg-8b -I/usr/local/cuda/include -I/usr/include/libdrm -I/usr/include/opencv4 -gencode arch=compute_72,code=sm_72 -m64 -ccbin g++ -g -G -O0 -MD -MT CMakeFiles/NvBufferCreateTest.dir/main.cpp.o -MF CMakeFiles/NvBufferCreateTest.dir/main.cpp.o.d -o CMakeFiles/NvBufferCreateTest.dir/main.cpp.o -c /home/user/NvBufferCreateTest/main.cpp

Hi,
It looks like the attributes of pthread trigger the issue. By creating pthread without attributes:

    pthread_create(&thread, NULL, &pthread, NULL);

The sample can run successfully.

And NvDestroyEGLImage() is not seen in the sample. There is memory leak without this function call.

So there it is not possible to get it running in the detached mode?

Hi,
We get same result. In the detached mode, the buffer and eglimage cannot be created successfully.

Not sure if this works but you may try to create buffers and eglimage in main thread and pass them to sub thread in the detached mode.