Deepstream Memory Leak

• Jetson Xavier NX
• deepstream 5.1
• JetPack Version 4.5.1

While using deepstream connected to a gst camera feed I get the following error after approximately 20 minutes:

PosixMemMap:71 [12] mmap failed
nvbufsurface: NvBufSurfaceMap function failed
nvbufsurface: mapping of buffer (0) failed
nvbufsurface: error in mapping
get_nvds_buf_Surface: Failed to map buffer to CPU
Segmentation fault (core dumped)

I stripped down my program to minimize any outside issues as the following which still produces the error:

def startVideoStream(self):
    # Standard GStreamer initialization
    GObject.threads_init()
    Gst.init(None)

    # Create gstreamer elements
    # Create Pipeline element that will form a connection of other elements
    print("Creating Pipeline \n ")
    pipeline = Gst.Pipeline()

    if not pipeline:
        sys.stderr.write(" Unable to create Pipeline \n")

    # Source element for reading from camera
    print("Creating Source \n ")
    source = Gst.ElementFactory.make("v4l2src", "usb-cam-source")
    if not source:
        sys.stderr.write(" Unable to create Source \n")

    caps_v4l2src = Gst.ElementFactory.make("capsfilter", "v4l2src_caps")
    if not caps_v4l2src:
        sys.stderr.write(" Unable to create v4l2src capsfilter \n")

    print("Creating Video Converter \n")

    # Adding videoconvert -> nvvideoconvert as not all
    # raw formats are supported by nvvideoconvert;
    # Say YUYV is unsupported - which is the common
    # raw format for many logi usb cams
    # In case we have a camera with raw format supported in
    # nvvideoconvert, GStreamer plugins' capability negotiation
    # shall be intelligent enough to reduce compute by
    # videoconvert doing passthrough (TODO we need to confirm this)


    # Use nvjpegdec for hardware accelerated decode on GPU
    print("Creating jpegdec \n")
    decoder = Gst.ElementFactory.make("jpegdec", "jpeg-decoder")
    if not decoder:
        sys.stderr.write(" Unable to create jpegdec Decoder \n")

    caps_vidconvsrc = Gst.ElementFactory.make("capsfilter", "nvmm_caps")
    if not caps_vidconvsrc:
        sys.stderr.write(" Unable to create capsfilter \n")

    # Create nvstreammux instance to form batches from one or more sources.
    streammux = Gst.ElementFactory.make("nvstreammux", "Stream-muxer")
    if not streammux:
        sys.stderr.write(" Unable to create NvStreamMux \n")

    # Use nvinfer to run inferencing on decoder's output,
    # behaviour of inferencing is set through config file
    pgie = Gst.ElementFactory.make("nvinfer", "primary-inference")
    if not pgie:
        sys.stderr.write(" Unable to create pgie \n")

    tracker = Gst.ElementFactory.make("nvtracker", "tracker")
    if not tracker:
        sys.stderr.write(" Unable to create tracker \n")

    # nvvideoconvert to convert incoming raw buffers to NVMM Mem (NvBufSurface API)
    nvvidconv = Gst.ElementFactory.make("nvvideoconvert", "convertor")
    if not nvvidconv:
        sys.stderr.write(" Unable to create nvvidconv \n")

    # Create OSD to draw on the converted RGBA buffer
    nvosd = Gst.ElementFactory.make("nvmultistreamtiler", "onscreendisplay")#nvdsosd

    if not nvosd:
        sys.stderr.write(" Unable to create nvosd \n")

    print("Creating EGLSink \n")
    sink = Gst.ElementFactory.make("fakesink", "nvvideo-renderer")
    if not sink:
        sys.stderr.write(" Unable to create egl sink \n")

    print("Playing cam /dev/video0")
    source.set_property('device', "/dev/video0")
    source.set_property('io-mode', 2)
    caps_v4l2src.set_property('caps', Gst.Caps.from_string("image/jpeg,width=640,height=480,framerate=30/1"))
    caps_vidconvsrc.set_property('caps', Gst.Caps.from_string("video/x-raw(memory:NVMM),format=RGBA"))
    streammux.set_property('width', self.resolution.width)
    streammux.set_property('height', self.resolution.height)
    streammux.set_property('live-source', 1)
    streammux.set_property('batch-size', 1)
    streammux.set_property('batched-push-timeout', 4000000)
    nvosd.set_property('rows', 1)
    nvosd.set_property('columns', 1)
    nvosd.set_property('width', 640)
    nvosd.set_property('height', 480)
    # Set sync = false to avoid late frame drops at the display-sink
    sink.set_property('sync', False)

    #Set properties of pgie and sgie
    pgie.set_property('config-file-path', "/home/drone1/Documents/GroundControl2/ConfigFiles/dstest2_pgie_config.txt")

    #Set properties of tracker
    config = configparser.ConfigParser()
    config.read('/home/drone1/Documents/GroundControl2/ConfigFiles/dstest2_tracker_config.txt')
    config.sections()

    for key in config['tracker']:
        if key == 'tracker-width' :
            tracker_width = config.getint('tracker', key)
            tracker.set_property('tracker-width', tracker_width)
        if key == 'tracker-height' :
            tracker_height = config.getint('tracker', key)
            tracker.set_property('tracker-height', tracker_height)
        if key == 'gpu-id' :
            tracker_gpu_id = config.getint('tracker', key)
            tracker.set_property('gpu_id', tracker_gpu_id)
        if key == 'll-lib-file' :
            tracker_ll_lib_file = config.get('tracker', key)
            tracker.set_property('ll-lib-file', tracker_ll_lib_file)
        if key == 'll-config-file' :
            tracker_ll_config_file = config.get('tracker', key)
            tracker.set_property('ll-config-file', tracker_ll_config_file)
        if key == 'enable-batch-process' :
            tracker_enable_batch_process = config.getint('tracker', key)
            tracker.set_property('enable_batch_process', tracker_enable_batch_process)
        if key == 'enable-past-frame' :
            tracker_enable_past_frame = config.getint('tracker', key)
            tracker.set_property('enable_past_frame', tracker_enable_past_frame)

    print("Adding elements to Pipeline \n")
    pipeline.add(source)
    pipeline.add(caps_v4l2src)
    pipeline.add(decoder)
    pipeline.add(nvvidconv)
    pipeline.add(caps_vidconvsrc)
    pipeline.add(streammux)
    pipeline.add(pgie)
    pipeline.add(tracker)
    pipeline.add(nvosd)
    pipeline.add(sink)
    #if is_aarch64():
        #pipeline.add(transform)

    # we link the elements together
    # file-source -> h264-parser -> nvh264-decoder ->
    # nvinfer -> nvvidconv -> nvosd -> video-renderer
    print("Linking elements in the Pipeline \n")
    self.link_element(source, caps_v4l2src)
    self.link_element(caps_v4l2src, decoder)
    self.link_element(decoder, nvvidconv)
    self.link_element(nvvidconv, caps_vidconvsrc)

    sinkpad = streammux.get_request_pad("sink_0")
    if not sinkpad:
        sys.stderr.write(" Unable to get the sink pad of streammux \n")
    srcpad = caps_vidconvsrc.get_static_pad("src")
    if not srcpad:
        sys.stderr.write(" Unable to get source pad of decoder \n")
    self.link_element(srcpad, sinkpad)
    self.link_element(streammux, pgie)
    self.link_element(pgie, tracker)
    self.link_element(tracker, nvosd)
    self.link_element(nvosd, sink)


    # create and event loop and feed gstreamer bus mesages to it
    loop = GObject.MainLoop()

    bus = pipeline.get_bus()
    bus.add_signal_watch()
    bus.connect ("message", bus_call, loop)

    # Lets add probe to get informed of the meta data generated, we add probe to
    # the sink pad of the osd element, since by that time, the buffer would have
    # had got all the metadata.
    osdsinkpad = nvosd.get_static_pad("sink")
    #osdsinkpad = caps_vidconvsrc.get_static_pad("sink")
    if not osdsinkpad:
        sys.stderr.write(" Unable to get sink pad of nvosd \n")
    osdsinkpad.add_probe(Gst.PadProbeType.BUFFER, self.osd_sink_pad_buffer_probe, 0)


    print("Starting pipeline \n")
    
    # start play back and listed to events
    pipeline.set_state(Gst.State.PLAYING)
    try:
        loop.run()
    except:
        pass

    # cleanup
    pipeline.set_state(Gst.State.NULL)

def osd_sink_pad_buffer_probe(self,pad,info,u_data,):
    meta_data_list = []
    frame_number=0
    #Intiallizing object counter with 0.
    obj_counter = {
        self.PGIE_CLASS_ID_VEHICLE:0,
        self.PGIE_CLASS_ID_PERSON:0,
        self.PGIE_CLASS_ID_BICYCLE:0,
        self.PGIE_CLASS_ID_ROADSIGN:0
    }
    num_rects=0
    gst_buffer = info.get_buffer()
    if not gst_buffer:
        print("Unable to get GstBuffer ")
        return

    # Retrieve batch metadata from the gst_buffer
    # Note that pyds.gst_buffer_get_nvds_batch_meta() expects the
    # C address of gst_buffer as input, which is obtained with hash(gst_buffer)
    batch_meta = pyds.gst_buffer_get_nvds_batch_meta(hash(gst_buffer))
    l_frame = batch_meta.frame_meta_list
    while l_frame is not None:
        try:
            # Note that l_frame.data needs a cast to pyds.NvDsFrameMeta
            # The casting is done by pyds.NvDsFrameMeta.cast()
            # The casting also keeps ownership of the underlying memory
            # in the C code, so the Python garbage collector will leave
            # it alone.
            frame_meta = pyds.NvDsFrameMeta.cast(l_frame.data)
        except StopIteration:
            break

        frame_number=frame_meta.frame_num
        num_rects = frame_meta.num_obj_meta
        l_obj=frame_meta.obj_meta_list
        obj_meta = None

        while l_obj is not None:
            try:
                # Casting l_obj.data to pyds.NvDsObjectMeta
                obj_meta=pyds.NvDsObjectMeta.cast(l_obj.data)
                
                if obj_meta is not None:
                    meta_data_list.append(obj_meta)
            except StopIteration:
                break
            obj_counter[obj_meta.class_id] += 1
            try: 
                l_obj=l_obj.next
            except StopIteration:
                break
        try:
            l_frame=l_frame.next
        except StopIteration:
            break

    # Getting Image data using nvbufsurface
    # the input should be address of buffer and batch_id
    n_frame = pyds.get_nvds_buf_surface(hash(gst_buffer), frame_meta.batch_id)
    
    return Gst.PadProbeReturn.OK

VideoProcessDeep copy.py (11.3 KB)

I was wondering if there is a way to clear the buffer to prevent this issue from happening or perhaps a workaround for the issue?

Thanks

ModuleNotFoundError: No module named ‘SystemConstants’

Seems you miss share somthing?

My mistake, I’ve removed the dependency. See the new attachment below.

VideoProcessDeep copy.py (11.2 KB)
dstest2_pgie_config.txt (3.3 KB)
dstest2_tracker_config.txt (1.8 KB)

Sorry for the long delay.
Is this still be an issue?

It is when I have an MJPEG camera as the source and my gst stream is configured for that input format. If I use a camera compatible with the deepstream USB example the memory issue does not present itself. Unfortunately the input device I need to use only outputs the MJPEG format.