Virtual Memory problem in code

Hello,

I’m creating a gst plugin that uses OpenCV to apply some filters, and based on the gst example plugin, I have this code that is making the Virtual Memory of the cgroup to explode after 50 minutes of processing 2 different RTSP sources at 15 fps.

for (guint i = 0; i < batch->frames.size (); i++) {

        GstMapInfo in_map_info;

        NvBufSurface *in_surf;  

        if (!gst_buffer_map(batch->inbuf, &in_map_info, GST_MAP_READ))

        {
          GST_ELEMENT_ERROR(clahe, STREAM, FAILED,
                            ("%s:gst buffer map to get pointer to NvBufSurface failed", __func__), (NULL));
          return FALSE;
        }
        in_surf = (NvBufSurface *)in_map_info.data;

        // Map the buffer so that it can be accessed by CPU
        if (NvBufSurfaceMap(in_surf, i, 0, NVBUF_MAP_READ_WRITE) != 0)
        {
          GST_ELEMENT_ERROR(clahe, STREAM, FAILED,
                            ("%s:buffer map to be accessed by CPU failed", __func__), (NULL));
          return FALSE;
        }
        // sync mapped data for CPU access
        NvBufSurfaceSyncForCpu(in_surf, i, 0);

        int h = in_surf->surfaceList[i].height;
        int w = in_surf->surfaceList[i].width;
        int p = in_surf->surfaceList[i].pitch;

        for (int j=0; j < h; j++) {
          memcpy(in_surf->surfaceList[i].mappedAddr.addr[0] + (j*p) , batch->cvmat[i].data + (j * w * 4), w * 4);
        }

        NvBufSurfaceSyncForDevice(in_surf, i, 0);
        if (NvBufSurfaceUnMap(in_surf, i, 0))
        {
          GST_ELEMENT_ERROR(clahe, STREAM, FAILED,
                            ("%s:buffer unmap to be accessed by CPU failed", __func__), (NULL));
          return FALSE;
        }
        gst_buffer_unmap(batch->inbuf, &in_map_info);
      }

Could you please help me to find out why this code is creating a memory leak? I´m not able to see any problem.

Thanks

Hi,
The code looks fine. Could you remove the memcpy and try again?

        for (int j=0; j < h; j++) {
          memcpy(in_surf->surfaceList[i].mappedAddr.addr[0] + (j*p) , batch->cvmat[i].data + (j * w * 4), w * 4);
        }

Probably data is copied out of range.

Hi,

Thanks for the answer, but if I remove this code, I will not have the frame available in surfaceList[i].mappedAddr.addr[0] copied into batch->cvmat[i].data. Is there any other method to copy data between them?

Thanks

Hi,
Would suggest remove some part in the code and see if the issue is still present. To find which function makes the leak. Or may remove NvBufSurfaceMap() and NvBufSurfaceUnMap for a try.

Hi, i´ve been doing some stuff to search for the issue. What i found is that:

  • memcpy line is not the problem, datais well copied
  • problem seems to be in the section when mapping/syncing the NvBufSurface.

Right now, i´ve found some code in this forum to change the way to doing this, using the buffer directly and copying it, but i´m getting core dump:

for (guint i = 0; i < batch->frames.size (); i++) {
        //cout << "Processing output " << i << "\n";
        GstMapInfo in_map_info = GST_MAP_INFO_INIT;
        NvBufSurface *in_surf;  
      
        if (!gst_buffer_map(batch->inbuf, &in_map_info, GST_MAP_WRITE))
        {
          GST_ELEMENT_ERROR(clahe, STREAM, FAILED,
                            ("%s:gst buffer map to get pointer to NvBufSurface failed", __func__), (NULL));
          return FALSE;
        }
        in_surf = (NvBufSurface *)in_map_info.data;
        
        int h = in_surf->surfaceList[i].height;
        int w = in_surf->surfaceList[i].width;
        int p = in_surf->surfaceList[i].pitch;
        
        for (int j=0; j < h; j++) {
          memcpy(in_surf->surfaceList[i].mappedAddr.addr[0] + (j*p) , batch->cvmat[i].data + (j * w * 4), w * 4);
        }
        gst_buffer_unmap(batch->inbuf, &in_map_info);
      }

This is what i have now, do you see why i´m getting the core dump? I cannot see the problem. Thanks

Hi,
Please print out the address of

in_surf->surfaceList[i].mappedAddr.addr[0]

Probably it is NULL and triggers core dump.

And you may make a patch to deepstream-test3 so that we can replicate the error and check. Do you run r32.4.4 + DS5.0.1? You can check the version by executing $ head -1 /etc/nv_Tegra_release.

Hello again,

It seems the problem is that there is a leak of 100kb in the NvBufSurfaceMap, since there is a post here talking about the same issue that has the first piece of code i posted here. All these code is working and it is in the gst-example, but this is to copy a CvMat into a NvBufSurface, in order to replace the original frame with the one of the Opencv matrix, which already has that frame with some filters applied.
Any help with the leak coming from the NvBufSurfaceMap/Unmap??
Thanks for the help

Hi,
We would need to replicate the issue for further investigation. Please make a patch to gstdsexample.cpp so that we can enable it and run deepstream-app to reproduce the issue.

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

Hi marceglozano,

Please make a patch to gstdsexample.cpp so that we can enable it and run deepstream-app to reproduce the issue.