Skip to content

Commit

Permalink
PR #11095 from Andor: Fix memory leaks in post processing filters
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Nov 14, 2022
2 parents b2b64fb + fc62833 commit 8dfd121
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 100 deletions.
9 changes: 7 additions & 2 deletions src/proc/decimation-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,16 @@ namespace librealsense
decimation_step,
decimation_default_val,
&_control_val, "Decimation scale");
decimation_control->on_set([this, decimation_control](float val)

auto weak_decimation_control = std::weak_ptr<ptr_option<uint8_t>>(decimation_control);
decimation_control->on_set([this, weak_decimation_control](float val)
{
auto strong_decimation_control = weak_decimation_control.lock();
if(!strong_decimation_control) return;

std::lock_guard<std::mutex> lock(_mutex);

if (!decimation_control->is_valid(val))
if (!strong_decimation_control->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported decimation scale " << val << " is out of range.");

Expand Down
15 changes: 0 additions & 15 deletions src/proc/disparity-transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,6 @@ namespace librealsense
_update_target(false),
_width(0), _height(0), _bpp(0)
{
auto transform_opt = std::make_shared<ptr_option<bool>>(
false,true,true,true,
&_transform_to_disparity,
"Stereoscopic Transformation Mode");
transform_opt->set_description(false, "Disparity to Depth");
transform_opt->set_description(true, "Depth to Disparity");
transform_opt->on_set([this, transform_opt](float val)
{
std::lock_guard<std::mutex> lock(_mutex);
if (!transform_opt->is_valid(val))
throw invalid_value_exception(to_string() << "Unsupported transformation mode" << (int)val << " is out of range.");

on_set_mode(static_cast<bool>(!!int(val)));
});

unregister_option(RS2_OPTION_FRAMES_QUEUE_SIZE);

on_set_mode(_transform_to_disparity);
Expand Down
9 changes: 6 additions & 3 deletions src/proc/hole-filling-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,17 @@ namespace librealsense
hole_filling_mode->set_description(hf_farest_from_around, "Farest from around");
hole_filling_mode->set_description(hf_nearest_from_around, "Nearest from around");

hole_filling_mode->on_set([this, hole_filling_mode](float val)
auto weak_hole_filling_mode = std::weak_ptr<ptr_option<uint8_t>>(hole_filling_mode);
hole_filling_mode->on_set([this, weak_hole_filling_mode](float val)
{
std::lock_guard<std::mutex> lock(_mutex);
auto strong_hole_filling_mode = weak_hole_filling_mode.lock();
if(!strong_hole_filling_mode) return;

if (!hole_filling_mode->is_valid(val))
if (!strong_hole_filling_mode->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported mode for hole filling selected: value " << val << " is out of range.");

std::lock_guard<std::mutex> lock(_mutex);
_hole_filling_mode = static_cast<uint8_t>(val);
});

Expand Down
15 changes: 8 additions & 7 deletions src/proc/pointcloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,14 @@ namespace librealsense

// Passing shared_ptr to capture list generates circular dependency and a memleak
auto occ_inv_weak = std::weak_ptr< ptr_option< uint8_t > >( occlusion_invalidation );
occlusion_invalidation->on_set( [this, occ_inv_weak]( float val ) {
if( auto occ_inv_shared = occ_inv_weak.lock() )
{
if( ! occ_inv_shared->is_valid( val ) )
throw invalid_value_exception( to_string() << "Unsupported occlusion filtering mode requiested "
<< val << " is out of range." );
}
occlusion_invalidation->on_set( [this, occ_inv_weak]( float val )
{
auto occ_inv_shared = occ_inv_weak.lock();
if(!occ_inv_shared) return;

if( ! occ_inv_shared->is_valid( val ) )
throw invalid_value_exception( to_string() << "Unsupported occlusion filtering mode requiested "
<< val << " is out of range." );

_occlusion_filter->set_mode(static_cast<uint8_t>(val));

Expand Down
19 changes: 13 additions & 6 deletions src/proc/spatial-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,18 @@ namespace librealsense
delta_step,
delta_default_val,
&_spatial_delta_param, "Edge-preserving Threshold");
spatial_filter_delta->on_set([this, spatial_filter_delta](float val)

auto weak_spatial_filter_delta = std::weak_ptr<ptr_option<uint8_t>>();
spatial_filter_delta->on_set([this, weak_spatial_filter_delta](float val)
{
std::lock_guard<std::mutex> lock(_mutex);
auto strong_spatial_filter_delta = weak_spatial_filter_delta.lock();
if(!strong_spatial_filter_delta) return;

if (!spatial_filter_delta->is_valid(val))
if (!strong_spatial_filter_delta->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported spatial delta: " << val << " is out of range.");

std::lock_guard<std::mutex> lock(_mutex);
_spatial_delta_param = static_cast<uint8_t>(val);
_spatial_edge_threshold = float(_spatial_delta_param);
});
Expand All @@ -111,14 +115,17 @@ namespace librealsense
holes_filling_mode->set_description(sp_hf_16_pixel_radius, "16-pixel radius");
holes_filling_mode->set_description(sp_hf_unlimited_radius, "Unlimited");

holes_filling_mode->on_set([this, holes_filling_mode](float val)
auto weak_holes_filling_mode = std::weak_ptr<ptr_option<uint8_t>>();
holes_filling_mode->on_set([this, weak_holes_filling_mode](float val)
{
std::lock_guard<std::mutex> lock(_mutex);
auto strong_holes_filling_mode = weak_holes_filling_mode.lock();
if(!strong_holes_filling_mode) return;

if (!holes_filling_mode->is_valid(val))
if (!strong_holes_filling_mode->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported mode for spatial holes filling selected: value " << val << " is out of range.");

std::lock_guard<std::mutex> lock(_mutex);
_holes_filling_mode = static_cast<uint8_t>(val);
switch (_holes_filling_mode)
{
Expand Down
18 changes: 12 additions & 6 deletions src/proc/synthetic-stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ namespace librealsense
std::weak_ptr<ptr_option<int>> stream_selector_ref = stream_selector;
stream_selector->on_set([this, stream_selector_ref](float val)
{
std::lock_guard<std::mutex> lock(_mutex);
auto stream_selector_strong_ref = stream_selector_ref.lock();
if(!stream_selector_strong_ref) return;

if (!stream_selector_ref.lock()->is_valid(val))
if (!stream_selector_strong_ref->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported stream filter, " << val << " is out of range.");

std::lock_guard<std::mutex> lock(_mutex);
_stream_filter.stream = static_cast<rs2_stream>((int)val);
});

Expand All @@ -190,25 +192,29 @@ namespace librealsense
std::weak_ptr<ptr_option<int>> format_selector_ref = format_selector;
format_selector->on_set([this, format_selector_ref](float val)
{
std::lock_guard<std::mutex> lock(_mutex);
auto format_selector_strong_ref = format_selector_ref.lock();
if(!format_selector_strong_ref) return;

if (!format_selector_ref.lock()->is_valid(val))
if (!format_selector_strong_ref->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported stream format filter, " << val << " is out of range.");

std::lock_guard<std::mutex> lock(_mutex);
_stream_filter.format = static_cast<rs2_format>((int)val);
});

auto index_selector = std::make_shared<ptr_option<int>>(-1, std::numeric_limits<int>::max(), 1, -1, &_stream_filter.index, "Stream index");
std::weak_ptr<ptr_option<int>> index_selector_ref = index_selector;
index_selector->on_set([this, index_selector_ref](float val)
{
std::lock_guard<std::mutex> lock(_mutex);
auto index_selector_strong_ref = index_selector_ref.lock();
if(!index_selector_strong_ref) return;

if (!index_selector_ref.lock()->is_valid(val))
if (!index_selector_strong_ref->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported stream index filter, " << val << " is out of range.");

std::lock_guard<std::mutex> lock(_mutex);
_stream_filter.index = (int)val;
});

Expand Down
17 changes: 13 additions & 4 deletions src/proc/temporal-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ namespace librealsense
temporal_persistence_control->set_description(7, "Valid in 1/8");
temporal_persistence_control->set_description(8, "Always on");

temporal_persistence_control->on_set([this,temporal_persistence_control](float val)
auto weak_temporal_persistence_control = std::weak_ptr<ptr_option<uint8_t>>(temporal_persistence_control);
temporal_persistence_control->on_set([this, weak_temporal_persistence_control](float val)
{
if (!temporal_persistence_control->is_valid(val))
auto strong_temporal_persistence_control = weak_temporal_persistence_control.lock();
if(!strong_temporal_persistence_control) return;

if (!strong_temporal_persistence_control->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported temporal persistence param "
<< (int)val << " is out of range.");
Expand All @@ -91,9 +95,14 @@ namespace librealsense
temp_delta_step,
temp_delta_default,
&_delta_param, "Edge-preserving (gradient) threshold");
temporal_filter_delta->on_set([this, temporal_filter_delta](float val)

auto weak_temporal_filter_delta = std::weak_ptr<ptr_option<uint8_t>>();
temporal_filter_delta->on_set([this, weak_temporal_filter_delta](float val)
{
if (!temporal_filter_delta->is_valid(val))
auto strong_temporal_filter_delta = weak_temporal_filter_delta.lock();
if(!strong_temporal_filter_delta) return;

if (!strong_temporal_filter_delta->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported temporal delta: " << val << " is out of range.");

Expand Down
81 changes: 24 additions & 57 deletions src/proc/zero-order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ namespace librealsense
return false;
}

template<typename T>
void register_on_set_callback_on(const std::shared_ptr<T>& p_option)
{
auto weak_p_option = std::weak_ptr<T>(p_option);
p_option->on_set([weak_p_option](float val)
{
auto strong_p_option = weak_p_option.lock();
if(!strong_p_option) return;

if (!strong_p_option->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported value for " << strong_p_option->get_description() << ": " << val << " is out of range.");
});
}

zero_order::zero_order(std::shared_ptr<bool_option> is_enabled_opt)
: generic_processing_block("Zero Order Fix"), _first_frame(true), _is_enabled_opt(is_enabled_opt),
_resolutions_depth { 0 }
Expand All @@ -169,13 +184,7 @@ namespace librealsense
115,
&_options.ir_threshold,
"IR threshold");
ir_threshold->on_set([ir_threshold](float val)
{
if (!ir_threshold->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported ir threshold " << val << " is out of range.");
});

register_on_set_callback_on(ir_threshold);
register_option(static_cast<rs2_option>(RS2_OPTION_FILTER_ZO_IR_THRESHOLD), ir_threshold);

auto rtd_high_threshold = std::make_shared<ptr_option<uint16_t>>(
Expand All @@ -185,13 +194,7 @@ namespace librealsense
200,
&_options.rtd_high_threshold,
"RTD high threshold");
rtd_high_threshold->on_set([rtd_high_threshold](float val)
{
if (!rtd_high_threshold->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported rtd high threshold " << val << " is out of range.");
});

register_on_set_callback_on(rtd_high_threshold);
register_option(static_cast<rs2_option>(RS2_OPTION_FILTER_ZO_RTD_HIGH_THRESHOLD), rtd_high_threshold);

auto rtd_low_threshold = std::make_shared<ptr_option<uint16_t>>(
Expand All @@ -201,13 +204,7 @@ namespace librealsense
200,
&_options.rtd_low_threshold,
"RTD high threshold");
rtd_low_threshold->on_set([rtd_low_threshold](float val)
{
if (!rtd_low_threshold->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported rtd low threshold " << val << " is out of range.");
});

register_on_set_callback_on(rtd_low_threshold);
register_option(static_cast<rs2_option>(RS2_OPTION_FILTER_ZO_RTD_LOW_THRESHOLD), rtd_low_threshold);

auto baseline = std::make_shared<ptr_option<float>>(
Expand All @@ -217,12 +214,7 @@ namespace librealsense
-10.f,
&_options.baseline,
"Baseline");
baseline->on_set([baseline](float val)
{
if (!baseline->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported patch size value " << val << " is out of range.");
});
register_on_set_callback_on(baseline);
register_option(static_cast<rs2_option>(RS2_OPTION_FILTER_ZO_BASELINE), baseline);

auto patch_size = std::make_shared<ptr_option<int>>(
Expand All @@ -232,12 +224,7 @@ namespace librealsense
5,
&_options.patch_size,
"Patch size");
patch_size->on_set([patch_size](float val)
{
if (!patch_size->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported patch size value " << val << " is out of range.");
});
register_on_set_callback_on(patch_size);
register_option(static_cast<rs2_option>(RS2_OPTION_FILTER_ZO_PATCH_SIZE), patch_size);

auto zo_max = std::make_shared<ptr_option<int>>(
Expand All @@ -247,12 +234,7 @@ namespace librealsense
1200,
&_options.z_max,
"ZO max value");
zo_max->on_set([zo_max](float val)
{
if (!zo_max->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported patch size value " << val << " is out of range.");
});
register_on_set_callback_on(zo_max);
register_option(static_cast<rs2_option>(RS2_OPTION_FILTER_ZO_MAX_VALUE), zo_max);

auto ir_min = std::make_shared<ptr_option<int>>(
Expand All @@ -262,12 +244,7 @@ namespace librealsense
75,
&_options.ir_min,
"Minimum IR value (saturation)");
ir_min->on_set([ir_min](float val)
{
if (!ir_min->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported patch size value " << val << " is out of range.");
});
register_on_set_callback_on(ir_min);
register_option(static_cast<rs2_option>(RS2_OPTION_FILTER_ZO_IR_MIN_VALUE), ir_min);

auto offset = std::make_shared<ptr_option<int>>(
Expand All @@ -277,12 +254,7 @@ namespace librealsense
10,
&_options.threshold_offset,
"Threshold offset");
offset->on_set([offset](float val)
{
if (!offset->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported patch size value " << val << " is out of range.");
});
register_on_set_callback_on(offset);
register_option(static_cast<rs2_option>(RS2_OPTION_FILTER_ZO_THRESHOLD_OFFSET), offset);

auto scale = std::make_shared<ptr_option<int>>(
Expand All @@ -292,12 +264,7 @@ namespace librealsense
20,
&_options.threshold_scale,
"Threshold scale");
scale->on_set([scale](float val)
{
if (!scale->is_valid(val))
throw invalid_value_exception(to_string()
<< "Unsupported patch size value " << val << " is out of range.");
});
register_on_set_callback_on(scale);
register_option(static_cast<rs2_option>(RS2_OPTION_FILTER_ZO_THRESHOLD_SCALE), scale);
}

Expand Down

0 comments on commit 8dfd121

Please sign in to comment.