Skip to content

Commit

Permalink
protect device_hub callback from crash using make()
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Dec 10, 2023
1 parent c40c4d6 commit 515eff9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
36 changes: 26 additions & 10 deletions src/device_hub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,43 @@ namespace librealsense
{
typedef rs2::devices_changed_callback<std::function<void(rs2::event_information& info)>> hub_devices_changed_callback;

device_hub::device_hub(std::shared_ptr<librealsense::context> ctx, int mask)
device_hub::device_hub( std::shared_ptr< librealsense::context > ctx, int mask )
: _ctx( ctx )
, _mask( mask )
{
_device_list = _ctx->query_devices(mask);
}

void device_hub::init( std::shared_ptr< device_hub > const & sptr )
{
_device_list = _ctx->query_devices( _mask );

_device_change_subscription = _ctx->on_device_changes(
[&, mask]( std::vector< std::shared_ptr< device_info > > const & /*removed*/,
std::vector< std::shared_ptr< device_info > > const & /*added*/ )
[&, liveliness = std::weak_ptr< device_hub >( sptr )](
std::vector< std::shared_ptr< device_info > > const & /*removed*/,
std::vector< std::shared_ptr< device_info > > const & /*added*/ )
{
std::unique_lock< std::mutex > lock( _mutex );
if( auto keepalive = liveliness.lock() )
{
std::unique_lock< std::mutex > lock( _mutex );

_device_list = _ctx->query_devices( mask );
_device_list = _ctx->query_devices( _mask );

// Current device will point to the first available device
_camera_index = 0;
if( _device_list.size() > 0 )
_cv.notify_all();
// Current device will point to the first available device
_camera_index = 0;
if( _device_list.size() > 0 )
_cv.notify_all();
}
} );
}

/*static*/ std::shared_ptr< device_hub > device_hub::make( std::shared_ptr< librealsense::context > const & ctx,
int mask )
{
std::shared_ptr< device_hub > sptr( new device_hub( ctx, mask ) );
sptr->init( sptr );
return sptr;
}

device_hub::~device_hub()
{
}
Expand Down
6 changes: 5 additions & 1 deletion src/device_hub.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ namespace librealsense
*/
class device_hub
{
device_hub( std::shared_ptr< context > ctx, int device_mask ); // private; use make()!
void init( std::shared_ptr< device_hub > const & );

public:
explicit device_hub(std::shared_ptr<librealsense::context> ctx, int mask = RS2_PRODUCT_LINE_ANY);
static std::shared_ptr< device_hub > make( std::shared_ptr< context > const &,
int device_mask = RS2_PRODUCT_LINE_ANY );

~device_hub();

Expand Down
8 changes: 4 additions & 4 deletions src/pipeline/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace librealsense
pipeline::pipeline(std::shared_ptr<librealsense::context> ctx) :
_ctx(ctx),
_dispatcher(10),
_hub(ctx, RS2_PRODUCT_LINE_ANY_INTEL),
_hub( device_hub::make( ctx, RS2_PRODUCT_LINE_ANY_INTEL )),
_synced_streams({ RS2_STREAM_COLOR, RS2_STREAM_DEPTH, RS2_STREAM_INFRARED, RS2_STREAM_FISHEYE })
{}

Expand Down Expand Up @@ -165,7 +165,7 @@ namespace librealsense
std::shared_ptr<device_interface> pipeline::wait_for_device(const std::chrono::milliseconds& timeout, const std::string& serial)
{
// pipeline's device selection shall be deterministic
return _hub.wait_for_device(timeout, false, serial);
return _hub->wait_for_device(timeout, false, serial);
}

std::shared_ptr<librealsense::context> pipeline::get_context() const
Expand Down Expand Up @@ -239,7 +239,7 @@ namespace librealsense
}

//hub returns true even if device already reconnected
if (!_hub.is_connected(*_active_profile->get_device()))
if (!_hub->is_connected(*_active_profile->get_device()))
{
try
{
Expand Down Expand Up @@ -300,7 +300,7 @@ namespace librealsense
}

//hub returns true even if device already reconnected
if (!_hub.is_connected(*_active_profile->get_device()))
if (!_hub->is_connected(*_active_profile->get_device()))
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion src/pipeline/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace librealsense

mutable std::mutex _mtx;
std::shared_ptr<profile> _active_profile;
device_hub _hub;
std::shared_ptr< device_hub > _hub;
std::shared_ptr<config> _prev_conf;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/rs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ NOEXCEPT_RETURN(, context)

rs2_device_hub* rs2_create_device_hub(const rs2_context* context, rs2_error** error) BEGIN_API_CALL
{
return new rs2_device_hub{ std::make_shared<librealsense::device_hub>(context->ctx) };
return new rs2_device_hub{ device_hub::make( context->ctx ) };
}
HANDLE_EXCEPTIONS_AND_RETURN(nullptr, context)

Expand Down

0 comments on commit 515eff9

Please sign in to comment.