DeepStream segmentation fault causes the program to crash

Please provide complete information as applicable to your setup.

• Hardware Platform (Jetson )
• DeepStream 6.2 Version
• JetPack Version (5.1.3)
• TensorRT Version (8.5.2.2)
• NVIDIA GPU Driver Version (valid for GPU only)
• Issue Type( bugs)
• How to reproduce the issue ?

When running the following code for a long time, a segmentation fault occasionally occurs. Please help me check whether the issue is with my code or a bug in the DeepStream SDK.

#include "deepstream.h"

#include <sstream> 

//#include <spdlog/spdlog.h>

#include <gstnvdsmeta.h>

#include <nvbufsurface.h>

#include <exception>

//#include "../util/singleton.h"

#include <opencv2/opencv.hpp>

#include <ros/console.h>

#include <ros/ros.h>

#include <filesystem>

#include <std_msgs/String.h>

#include <modules/util/psdk_util.h>

#include "wrapper/wrapper.h"





using namespace deepstream;



std::mutex g_mtxFocusdata;

std::vector<cv::Mat> g_VecFocusmat = std::vector<cv::Mat>();



// �����ŶȽ������еıȽϺ���

bool compareConfidence(const NvDsObjectMeta\* a, const NvDsObjectMeta\* b) {

    return a->confidence > b->confidence;

}







static GstPadProbeReturn osd_sink_pad_buffer_probe (GstPad \* pad, GstPadProbeInfo \* info, gpointer u_data)

{

  

  try

  {

    if(PSDK_Wrapper::get_instance()->m_bTaskState)

    {

      GstBuffer \*buf = (GstBuffer \*)info->data;

      if(!buf)

      {

        USER_LOG_INFO("osd_sink_pad_buffer_probe-- buf is null");

        return GST_PAD_PROBE_OK;

      }

      

      

      GstMapInfo in_map_info;

      

      NvBufSurface \*surface = NULL;

  

      NvDsObjectMeta \*obj_meta = NULL;

     

      NvDsMetaList \* l_frame = NULL;

      NvDsMetaList \* l_obj = NULL;

  

    if (!gst_buffer_map (buf, &in_map_info, GST_MAP_READ))

      {

          g_print ("osd_sink_pad_buffer_probe-- gst_buffer_map() error!");

          USER_LOG_INFO("osd_sink_pad_buffer_probe-- gst_buffer_map() error");

          //gst_buffer_unmap(buf, &in_map_info);

          return GST_PAD_PROBE_OK;

      }

  

  

      surface =  (NvBufSurface \*) in_map_info.data;

      if(!surface)

      {

        USER_LOG_INFO("osd_sink_pad_buffer_probe-- surface is null");

        return GST_PAD_PROBE_OK;

      }

    

      NvDsBatchMeta \*batch_meta = gst_buffer_get_nvds_batch_meta (buf);

  

      if(!batch_meta)

      {

        USER_LOG_INFO("osd_sink_pad_buffer_probe-- batch_meta is null");

        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);

          if(!frame_meta)

          {

            //spdlog::info("osd_sink_pad_buffer_probe-- frame_meta is null");

            break;

          }

          if(frame_meta->num_obj_meta<=0){

             //ROS_INFO("No objects were detected.");

             //spdlog::info("osd_sink_pad_buffer_probe-- No objects were detected.");

             break;

          }

            

          

       

          std::vector<NvDsObjectMeta\*> label_J_objects;

       

          std::vector<NvDsObjectMeta\*> other_objects;

  

          for (l_obj = frame_meta->obj_meta_list; l_obj != NULL; l_obj = l_obj->next)

            {

              if(!l_obj->data){

                  USER_LOG_INFO("osd_sink_pad_buffer_probe-- l_obj data is null");

                  continue;

              }

              obj_meta = (NvDsObjectMeta \*) (l_obj->data);

              if(!obj_meta)

              {

                USER_LOG_INFO("osd_sink_pad_buffer_probe-- obj_meta is null");

                break;

              }

              std::string label = obj_meta->obj_label;

              if (label == "JianGeBang" ) {

                 if(PSDK_Wrapper::get_instance()->isJGBFly){

                 

                    label_J_objects.push_back(obj_meta);

                 }

                    

              } else {

                 other_objects.push_back(obj_meta);

              }

     

            }

          

          //���������Ŀ��

          if(label_J_objects.size() > 0){

            //�������3������ȡ���Ŷ���ߵ�3��

             if(label_J_objects.size() > 3){

                std::sort(label_J_objects.begin(), label_J_objects.end(), compareConfidence);

                label_J_objects.resize(3);

             }

  

  

             // ����ÿ�����󣬻�ȡ x, y, w, h ��ƴ�ӳ��ַ���

            std::string result;

            for (const auto& obj : label_J_objects) {

                  std::ostringstream oss;

                  oss << std::fixed << std::setprecision(2) << obj->rect_params.left << ","

                      << std::fixed << std::setprecision(2) << obj->rect_params.top << ","

                      << std::fixed << std::setprecision(2) << obj->rect_params.width << ","

                      << std::fixed << std::setprecision(2) << obj->rect_params.height << ","

                      << std::fixed << std::setprecision(2) << obj->confidence << ";";

                  result += oss.str();

                  USER_LOG_INFO("Object: %s, Probability: %.2f, Box: (%.2f, %.2f, %.2f, %.2f)", 

                obj->obj_label, obj->confidence, obj->rect_params.left, obj->rect_params.top, obj->rect_params.width, obj->rect_params.height);

            }

  

  

            std_msgs::String msg;

            msg.data = result;

            if(PSDK_Wrapper::get_instance()->pubJianGeBang_position)

            {

              PSDK_Wrapper::get_instance()->pubJianGeBang_position->publish(msg);

            }

       

            PSDK_Wrapper::get_instance()->setResult(result);

            

          }

  

    }

      gst_buffer_unmap (buf, &in_map_info);

    }

  }

  catch(const std::exception& e)

  {

    USER_LOG_INFO("osd_sink_pad_buffer_probe std::exception in");

    USER_LOG_INFO("error info :{}",e.what());

  }

  catch (...) {

    USER_LOG_INFO("osd_sink_pa_budffer_probe unknow error");

  }

  

    return GST_PAD_PROBE_OK;

}





static void LogFunction(GstDebugCategory \*category, GstDebugLevel level,

                        const gchar \*file, const gchar \*function, gint line,

                        GObject \*object, GstDebugMessage \*message,

                        gpointer user_data) {

  //spdlog::info("{}:{}", gst_debug_message_get(message), user_data);

  USER_LOG_INFO("%s:%p", gst_debug_message_get(message), user_data);

};




bool DeepStream::initPipe(std::string model_path, int interval)

{

  pipeline = gst_pipeline_new("pipeline");

  if (!pipeline) {

    USER_LOG_INFO("failed to gst_pipeline_new");

    return false;

  }

  src = gst_element_factory_make("appsrc", "src");



  if (!src) {

    USER_LOG_INFO("failed to gst_element_factory_make appsrc ");

     return false;

  }



  queue = gst_element_factory_make("queue", "queue");



  if (!queue) {

    USER_LOG_INFO("failed to gst_element_factory_make queue ");

        return false;

  }



  parser = gst_element_factory_make("h264parse", "parse");



  if (!parser) {

    USER_LOG_INFO("failed to gst_element_factory_make h264parse ");

       return false;

  }



  decoder = gst_element_factory_make("nvv4l2decoder", "decoder");



  if (!decoder) {

    USER_LOG_INFO("failed to gst_element_factory_make nvv4l2decoder ");

      return false;

  }



  convert = gst_element_factory_make("nvvideoconvert", "convert");



  if (!convert) {

    USER_LOG_INFO("failed to gst_element_factory_make nvvideoconvert ");

     return false;

  }



  



  mux = gst_element_factory_make("nvstreammux", "stream-muxer");



  if (!mux) {

    USER_LOG_INFO("failed to gst_element_factory_make mux ");

     return false;

  }



  infer =

      gst_element_factory_make("nvinfer", "primary-nvinference-engine");



  if (!infer) {

    USER_LOG_INFO("failed to gst_element_factory_make nvinfer ");

    return false;

  }



  convert2= gst_element_factory_make ("nvvideoconvert", "converter2");

  if(!convert){

     USER_LOG_INFO("failed to gst_element_factory_make nvvideoconvert ");

      return false;

  }



  /\* Create OSD to draw on the converted RGBA buffer \*/

  nvosd = gst_element_factory_make ("nvdsosd", "nv-onscreendisplay");

  if(!nvosd){

     USER_LOG_INFO("failed to gst_element_factory_make nvdsosd ");

      return false;

  }



  sink = gst_element_factory_make("fakesink", "sink");



  GstPad \*osd_sink_pad = gst_element_get_static_pad(this->nvosd, "sink");

  

  gst_pad_add_probe(osd_sink_pad, GST_PAD_PROBE_TYPE_BUFFER,

                      osd_sink_pad_buffer_probe, NULL, NULL);

            



  g_object_set(src, "stream-type", GST_APP_STREAM_TYPE_STREAM, "format",

               GST_FORMAT_TIME, "do-timestamp", TRUE, "is-live", TRUE, "block",

               FALSE, nullptr);



  g_object_set(decoder, "enable-max-performance", TRUE,

               "enable-frame-type-reporting", FALSE, "enable-error-check",

               FALSE, "disable-dpb", TRUE, nullptr);





  g_object_set(infer,

                 "config-file-path", model_path.c_str(),

         "interval",interval,

                  NULL);

    g_object_set(mux,

                 "width", 1920,

                 "height", 1200,

         //"width", 640,

                 //"height", 640,

                 "batch-size", 1,

                 "compute-hw", 1,

                 "interpolation-method", 6,

                 "live-source",TRUE,

                 nullptr);

  

  

  gst_bin_add_many(GST_BIN(pipeline), src, queue, parser, decoder, convert,mux, infer, convert2, nvosd, sink, nullptr);



  if (gst_element_link_many(src, queue, parser, decoder,nullptr)==false){

    USER_LOG_INFO("failed to link element from mux to dst1");

    return false;

  }



  if (gst_element_link_many(mux, infer, convert2, nvosd, sink,

                            nullptr) == false) {

    USER_LOG_INFO("failed to link element from convert2 to nvosd");

    return false;

  }



    GstPad\* decoder_src_pad=gst_element_get_static_pad(decoder,"src");

    if (!decoder_src_pad)

    {

        USER_LOG_INFO("tee request src1 pad failed. Exiting.");

    return false;

    }



    GstPad \*mux_sink0_pad = gst_element_get_request_pad(mux, "sink_0");

    if (!mux_sink0_pad)

    {

        USER_LOG_INFO("queue1 request sink pad failed. Exiting");

    return false;

    }




    if(gst_pad_link(decoder_src_pad,mux_sink0_pad)!=GST_PAD_LINK_OK){

        USER_LOG_INFO("Failed to link tee_src0 and queue1_sink Exiting.");

    return false;

    }

  



  ROS_INFO("\*\*\*\*\*\*\*\*\*\*\*\*initPipe OK \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*");

  return true;

}



void DeepStream::unPipeData()

{

   gst_object_unref(GST_OBJECT(pipeline));

  // gst_object_unref(pipeline);

  // pipeline = nullptr;

  // gst_object_unref(dst1);

  // gst_object_unref(dst2);

  // gst_object_unref(src);

  // g_main_loop_unref(loop);

  // gst_object_unref(mux);

  // gst_object_unref(infer);

  // gst_object_unref(convert2);

  // gst_object_unref(nvosd);

  // gst_object_unref(encoder1);

  // gst_object_unref(parser1);

  // gst_object_unref(flv1);

  // gst_object_unref(queue);

  // gst_object_unref(parser);

  // gst_object_unref(decoder);

  // gst_object_unref(convert);

}

DeepStream::DeepStream() {

  gst_init(NULL, NULL);

  gst_debug_add_log_function(LogFunction, NULL, NULL);

  gst_debug_set_default_threshold(GstDebugLevel::GST_LEVEL_ERROR);

  

}



DeepStream::\~DeepStream() {

  unPipeData();

}



bool DeepStream::start(std::string model_path, int interval) {

  if (is_living || is_starting) {

   USER_LOG_INFO("already start or in starting status");

    return true;

  }

  is_starting=true;

  USER_LOG_INFO("init dji module success initPipe 01");

  if(!initPipe(model_path, interval))

  {

    is_starting=false;

    return false;

  }

  USER_LOG_INFO("init dji module success initPipe 02");



  auto s = gst_element_set_state(pipeline, GST_STATE_PLAYING);

  if (s == GST_STATE_CHANGE_FAILURE) {

   USER_LOG_INFO("failed to set state to playing:{}",

                 gst_element_state_change_return_get_name(s));

    is_starting=false;

    return false;

  }



  



  is_StreamInited=true;

  is_living=true;

  is_starting=false;

  return true;

}



void DeepStream::send(const uint8_t \*data, int dataLen) {

  //ROS_INFO("send 000\*\*\*\*\*\* ");

  if(!is_living)

    return;

  GstBuffer \*gstDecodeBuffer;

    GstFlowReturn ret;

  //ROS_INFO("send 111\*\*\*\*\*\* ");

     gstDecodeBuffer = gst_buffer_new_allocate(nullptr, dataLen, nullptr);

     gst_buffer_fill(gstDecodeBuffer, 0, data, dataLen);

     ret = gst_app_src_push_buffer(GST_APP_SRC(src), gstDecodeBuffer);

     if (ret != GST_FLOW_OK) {

    gst_buffer_unref(gstDecodeBuffer);

        USER_LOG_INFO("Gstreamer appSrc push buffer error!");

     }

   //ROS_INFO("send OK\*\*\*\*\*\* ");

}



bool DeepStream::stop() {

  if (is_exiting) {

    USER_LOG_INFO("already stop or in stoping status");

    return true;

  }

  is_exiting = true;

  bool result = true;

  is_living = false;

  unPipeData();

  is_exiting = false;

  return result;

}

• Requirement details( This is for new requirement. Including the module name-for which plugin or for which sample application, the function description)

First things first, please use ``` when copy-pasting stack traces or code (I’ve edited your post to use that)
otherwise it’s not really readable.

Unable to judge the bug from the code, some tips:
1.Rebuild you program with -g compile options, so that gdb can parse the callstack.
2.Use systemd-coredump to collect program crash callstack.

There is no update from you for a period, assuming this is not an issue anymore. Hence we are closing this topic. If need further support, please open a new one. Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.