Release resources from eglstream consumer

We call the following function on the consumer when the producer closes the connection.
But I noticed that the memory goes up every time the producer reconnects.
Realizing that the context was not freed, I called cuCtxDestroy.
However, a segmentation fault occurs.
Instead of giving g_usleep and then calling it, it works normally.
What is the reason and is there an api that can replace g_usleep?

if (client->stream != EGL_NO_STREAM_KHR)
    EGLint state = 0;
    eglQueryStreamKHR(g_display, client->stream,
                      EGL_STREAM_STATE_KHR, &state);

    // Release the acquired frame if the stream hasn't disconnected yet.
        if (!eglStreamConsumerReleaseKHR(g_display,
            printf("Release frame failed.\n");

    if (!eglDestroyStreamKHR(g_display, client->stream))
        printf("Couldn't destroy EGL stream\n");
    client->stream = EGL_NO_STREAM_KHR;

    /* client->fd can only be valid if stream was valid */
    if (client->fd != EGL_NO_FILE_DESCRIPTOR_KHR)
        client->fd = EGL_NO_FILE_DESCRIPTOR_KHR;
    g_usleep(1000 * 1000);
    CUresult cure = cuCtxDestroy(client->cuda_consumer.context);
    printf("CU RESULT: %d\n", cure);