NvBufferMemMap() Pointer clean up

Hello,

I was wondering if the pointer I get back from the NvBufferMemMap() can be freed just using free() or do I need some special clean up for it ?

Hi,
Please call this API to unmap the memory:

/**
* Unmaps the mapped virtual address of the plane.
*
* If the following conditions are both true, the client must call
* NvBufferMemSyncForDevice() before unmapping the memory:
* - Mapped memory was modified by the CPU.
* - Mapped memory will be accessed by a hardware device.
*
* @param[in] dmabuf_fd  DMABUF FD of the buffer.
* @param[in] plane      Video frame plane. Applies to
*                       @ref NvBufferPayload_SurfArray.
* @param[in] pVirtAddr  Virtual address pointer to the memory-mapped plane.
*
* @returns 0 for success, -1 for failure.
*/
int NvBufferMemUnMap (int dmabuf_fd, unsigned int plane, void **pVirtAddr);

I currently have the following code to decode an image into a cv::Mat

cv::Mat JpegDecoder::DecodeToCvMat(std::string file_path, cv::ColorConversionCodes conv_code) {
    int fd = 0;
    int nvbuffer = 0;
    uint32_t width, height, pixfmt;
    void *pdata = NULL;

    std::ifstream in_file = std::ifstream(file_path);
    uint64_t in_file_size = get_file_size(&in_file);
    unsigned char in_buffer[in_file_size];

    in_file.read((char *) in_buffer, in_file_size);

    jpegdec->decodeToFd(fd, in_buffer, in_file_size, pixfmt,
        width, height);

    NvBufferCreateParams params;
    params.width = width;
    params.height = height;
    params.layout = NvBufferLayout_Pitch;
    params.colorFormat = NvBufferColorFormat_ABGR32;
    params.nvbuf_tag = NvBufferTag::NvBufferTag_JPEG;
    params.payloadType = NvBufferPayloadType::NvBufferPayload_SurfArray;

    NvBufferCreateEx(&nvbuffer, &params);

    NvBufferTransform(fd, nvbuffer, &transform_params);

    NvBufferMemMap(nvbuffer, 0, NvBufferMem_Read_Write, &pdata);

    NvBufferMemSyncForCpu(nvbuffer, 0, &pdata);

    cv::Mat nv_buf_mat = cv::Mat(height, width, CV_8UC4, pdata);

    cv::Mat output_mat;
    cv::cvtColor(nv_buf_mat, output_mat, conv_code);

    NvBufferMemUnMap(nvbuffer, 0, &pdata);

    in_file.close();
    return output_mat;
}```

But there is a memory leak somewhere because if I call it in a loop just decoding images into cv::Mat() which gets destroyed immediately after making it I run out of memory.  
That is why I am asking if there are any extra steps I need to take in order to free the memory besides the call to NvBufferMemUnMap or maybe there is something else wrong with my code for decoding.

Hi,
We only see NvBufferCreateEx() in the code. Please make sure you also call NvBufferDestroy() in pair. Or call NvBufferCreateEx() once and reuse the NvBuffer.

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