Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointcloud processing block causing memory leak #2860

Closed
mf-bwf opened this issue Dec 7, 2018 · 5 comments
Closed

pointcloud processing block causing memory leak #2860

mf-bwf opened this issue Dec 7, 2018 · 5 comments
Assignees

Comments

@mf-bwf
Copy link

mf-bwf commented Dec 7, 2018


Required Info
Camera Model D400
Firmware Version 05.10.06.00
Operating System & Version Ubuntu 16.04
Kernel Version (Linux Only) 4.15.0-42-generic
Platform PC
SDK Version v2.16.5
Language c++
Segment any

Issue Description

There seems to be an issue with memory deallocation for rs2::pointcloud
The following minimum example code results in a memory leak.

The memory leak can be made smaller by moving the definition of 'pc' outside of the while loop, keeping the instance alive until function exit.

void minimumTest()
{    
    rs2::pipeline pipe;
    cv::Mat pc_mat;

    // start stream
    rs2::pipeline_profile profile = pipe.start();

    // run for a while
    int i = 0;
    while(i < 10000)
    {
        // get a set of frames
        rs2::frameset update = pipe.wait_for_frames();
        rs2::frame depth = update.get_depth_frame();

        if(depth)
        {
            // create pointcloud
            rs2::pointcloud pc;
            rs2::points points = pc.calculate(depth);
            cv::Mat(720, 1280, CV_32FC3, (void*)points.get_data()).copyTo(pc_mat);

            // display pointcloud
            cv::imshow("minimum", pc_mat);
            if(cv::waitKey(1) > 0)
            {
                std::cout << "bye" << std::endl;
                break;
            }
        }
        i++;
    }
}
@dorodnic
Copy link
Contributor

dorodnic commented Dec 9, 2018

Hi @mf-bwf
Thanks for the report.
The way it works, every instance of pointcloud is generating a stream-profile that gets registered within the global extrinsics graph. The profile itself gets deleted (via a smart-pointer), but the instance of weak_ptr to it in the extrinsics graph remains until an explicit clean-up. These happen whenever you re-connect the device. We could trigger these also for every creation / destruction of a processing block to solve this issue.
However, creating the pointcloud object within a loop is not recommended (for anything except reproducing the leak). In most cases, you will be Ok with just one (or one per device).

@denkywu
Copy link

denkywu commented Apr 26, 2019

I have the same problem, and also solve the problem using the same way.
SDK Version: 2.19.0.570

@OneQuestion
Copy link

So, exactly, what is the proper way of avoiding any such memory leak? What is the procedure of destroying processing block?

@kmateti
Copy link

kmateti commented Mar 22, 2021

Hi @mf-bwf
Thanks for the report.
The way it works, every instance of pointcloud is generating a stream-profile that gets registered within the global extrinsics graph. The profile itself gets deleted (via a smart-pointer), but the instance of weak_ptr to it in the extrinsics graph remains until an explicit clean-up. These happen whenever you re-connect the device. We could trigger these also for every creation / destruction of a processing block to solve this issue.
However, creating the pointcloud object within a loop is not recommended (for anything except reproducing the leak). In most cases, you will be Ok with just one (or one per device).

Hi,

I was wondering if there was any update on this issue? I have found the memory leak even without any pointcloud instance, shown here (running v2.33.1):

#include <librealsense2/rs.hpp>
#include <iostream>

int main()
{
    rs2::pipeline pipe;
    rs2::config cfg;
    cfg.enable_device_from_file(<insert bag file path here>);
    pipe.start(cfg);
    auto frames = pipe.wait_for_frames();
    auto depth = frames.get_depth_frame();
    long int i = 0;
    while (1)
    {
        i++;
        std::cout << i << std::endl;
        frames = pipe.wait_for_frames();
        depth = frames.get_depth_frame();
    }
}

@ev-mp
Copy link
Collaborator

ev-mp commented Aug 4, 2021

Fix is in the development branch via #9553

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants