librealsense: Memory leak on restarting streams
SDK Version: v2.38.1.2223 OS: Windows 10 Home 64 bit Platform: Laptop (Intel Core i7-10510U, 16G RAM, NVIDIA GeForce MX250 2G) Language: C++ (Visual Studio 2019) Segment: Desktop Camera Model: D435i Firmware Version: 05.12.07.100
Issue Description
Recently we noticed some memory issues in our project. Each time we make restarts in our camera controller to change configuration, we face memory leak. We also tested our use case on different versions of RealSense SDK, but the problem is still there.
Sample to reproduce the Issue
main.cpp:
#include <librealsense2/rs.hpp>
#include <iostream>
static const int restarts = 200;
static int restarts_count = 0;
static const int frames_to_restart = 5;
// Global RS variables
rs2::context g_context;
std::shared_ptr<rs2::pipeline> g_pipeline = std::make_shared<rs2::pipeline>();
std::shared_ptr<rs2::config> g_config = std::make_shared<rs2::config>();
class StreamDataController {
public:
void init() {
auto devices = g_context.query_devices();
if (devices.size() == 0)
throw "Realsense2DepthProvider: Can't enumerate Realsense2 camera";
g_config->enable_stream(RS2_STREAM_DEPTH, 640, 480, RS2_FORMAT_Z16, 30);
}
void release() {
g_config->disable_all_streams();
}
void start() {
g_pipeline->start(*g_config);
}
void stop() {
g_pipeline->stop();
}
void handle() {
int frame_count = 0;
while (frame_count < frames_to_restart) {
auto frames = g_pipeline->wait_for_frames();
auto depth_frame = frames.get_depth_frame();
// Get the depth frame's dimensions
float width = depth_frame.get_width();
float height = depth_frame.get_height();
// Query the distance from the camera to the object in the center of the image
float dist_to_center = depth_frame.get_distance(width / 2, height / 2);
std::cout << "<Depth sensor> frame=" << frame_count << "\tThe camera is facing an object " << dist_to_center << " meters away" << std::endl;
++frame_count;
}
}
};
int main()
{
for (; restarts_count <= restarts; ++restarts_count) {
std::cout << "START REALSENSE #" << restarts_count << std::endl;
try {
StreamDataController controller;
controller.init();
controller.start();
controller.handle();
controller.stop();
controller.release();
}
catch (const rs2::error& e) {
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
}
}
std::cin.get();
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5.0)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(PROJECT_NAME rs_leak_test)
project(${PROJECT_NAME})
set(CMAKE_CONFIGURATION_TYPES Release)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Realsense SDK
set(RS_INSTALL_DIR "C:\\Program Files (x86)\\Intel RealSense SDK 2.0")
set(RS_INCLUDE_DIR "${RS_INSTALL_DIR}\\include")
set(RS_BIN_DIR "${RS_INSTALL_DIR}\\bin\\${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}")
set(RS_BINS "${RS_BIN_DIR}\\realsense2.dll")
set(RS_LIB_DIR "${RS_INSTALL_DIR}\\lib\\${CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE}")
set(RS_LIBS realsense2)
if(NOT EXISTS ${RS_INSTALL_DIR})
message("There's no such dir as ${RS_INSTALL_DIR}" FATAL_ERROR)
endif()
include_directories(${RS_INCLUDE_DIR})
link_directories(${RS_LIB_DIR})
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${RS_LIBS})
add_custom_command(TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${RS_BINS} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CONFIGURATION_TYPES}/)
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 43
I have highlighted the case to Intel again. This is the only course of action I can take, as the problem is outside of my own programming knowledge unfortunately. I do apologize.
The issue should be addressed in the latest SDK release 2.48.0. https://github.com/IntelRealSense/librealsense/wiki/Release-Notes#release-2480 Please take a look. Thank you.
A bug has been filed to track this issue internally. It appears the issue is also related to the camera model. The mem leak happens on D455 and D435i but not on D435 or D415. Thank you.