librealsense icon indicating copy to clipboard operation
librealsense copied to clipboard

What is the appropriate scope for rs2::context?

Open evercoast-dev opened this issue 7 years ago • 3 comments

Camera Model: D400 Firmware Version: 5.10.03+ Operating System: Ubuntu Kernel Version: 4.15.0-43-generic Platform: PC SDK Version: 2.19.0+ Language: C++ Segment: VR/AR

Best Practice Q: What is the appropriate scope of an rs2::context? Is it best to construct a context only once per application execution, or can a context be constructed on the stack at any point it's needed?

Most of the supplied examples are quite small applications. I ask in the context of a more complex application.

Thank you for your advice!

evercoast-dev avatar May 02 '19 16:05 evercoast-dev

Hi @evercoast-dev context is mainly used as device factory. It does not own device objects it helped to create (you can delete context as soon as you are done with it). It also, at least as far as I remember, is fairly trivially constructed, so in theory you could always create context on the stack.

dorodnic avatar May 03 '19 18:05 dorodnic

That said, device class does have some software state and every call to query_devices will result in a new device object instance. Hence, if you are accessing the device from different points in your application it might be a good idea to create it as soon as possible and share within your application.

dorodnic avatar May 03 '19 18:05 dorodnic

Excellent, thank you!

evercoast-dev avatar May 03 '19 20:05 evercoast-dev