Running image save in deepstream5 app issue

I try to fix several code from ajayskabadi2012 code and I have an error too.

Here is my code

static GstPadProbeReturn
gie_img_prob_new (GstPad * pad, GstPadProbeInfo * info, gpointer u_data)
{
    GstBuffer *buf = (GstBuffer *) info->data;
    GstMapInfo inmap = GST_MAP_INFO_INIT;
    NvDsObjEncCtxHandle ctx = (NvDsObjEncCtxHandle) u_data;

    NvBufSurface *ip_surf = (NvBufSurface *) inmap.data;
    gst_buffer_unmap (buf, &inmap);

    NvDsObjectMeta *obj_meta = NULL;
    NvDsMetaList *l_frame = NULL;
    NvDsMetaList *l_obj = NULL;
    NvDsBatchMeta *batch_meta = gst_buffer_get_nvds_batch_meta (buf);

    if (!gst_buffer_map (buf, &inmap, GST_MAP_READ)) {
        GST_ERROR ("input buffer mapinfo failed");
        return GST_PAD_PROBE_OK;
    }

    for (l_frame = batch_meta->frame_meta_list; l_frame != NULL;
        l_frame = l_frame->next) {
        NvDsFrameMeta *frame_meta = (NvDsFrameMeta *) (l_frame->data);

        for (l_obj = frame_meta->obj_meta_list; l_obj != NULL; l_obj = l_obj->next) {
            obj_meta = (NvDsObjectMeta *) (l_obj->data);

            /* Conditions that user needs to set to encode the detected objects of
            * interest. Here, by default all the detected objects are encoded.
            * For demonstration, we will encode the first object in the frame */
            if (obj_meta->class_id == 0 || obj_meta->class_id == 2 || obj_meta->class_id == 3 || obj_meta->class_id == 5 || obj_meta->class_id == 7) {
                printf("%d\n", obj_meta->class_id);
                NvDsObjEncUsrArgs userData = { 0 };
                /* To be set by user */
                userData.saveImg = FALSE;
                userData.attachUsrMeta = TRUE;
                /* Preset */
                userData.objNum = 1;
                /*Main Function Call */
                **nvds_obj_enc_process (ctx, &userData, ip_surf, obj_meta, frame_meta);**
            }
        }
    }
    nvds_obj_enc_finish (ctx);

    return GST_PAD_PROBE_OK;
}

and here is code inside create_common_elements function.

NvDsObjEncCtxHandle obj_ctx_handle = nvds_obj_enc_create_context();
if (!obj_ctx_handle) {
    g_print ("Unable to create context\n");
    return -1;
}
NVGSTDS_ELEM_ADD_PROBE (pipeline->common_elements.primary_bbox_buffer_probe_id,
        pipeline->common_elements.primary_gie_bin.bin, "src",
        gie_img_prob_new, GST_PAD_PROBE_TYPE_BUFFER,
        obj_ctx_handle);

Other code works very well but when it refer to ‘ctx’, it cause segmentation fault.

The codes you post seem OK. But in Running image save in deepstream5 app issue codes, there are two different probe functions with the same element’s same pad, have you fixed the error?

Thanks for your review

I just use ds-exalple to save cropped image.