Repository navigation
Create a singleton wrapper to rs2::context - #3430
Conversation
a891ab3 to
82955f0
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a singleton pattern for the rs2::context to ensure only one RealSense SDK context exists when running in a composable node environment. This change helps prevent resource conflicts when multiple components need to access shared RealSense resources.
- Creates a singleton wrapper class for rs2::context
- Replaces direct context usage with singleton access pattern
- Updates header includes to use relative paths instead of absolute paths
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| realsense2_camera/include/context_singleton_wrapper.h | New singleton wrapper class for rs2::context |
| realsense2_camera/src/realsense_node_factory.cpp | Updates context usage to use singleton pattern and fixes header includes |
| realsense2_camera/include/realsense_node_factory.h | Removes direct rs2::context member variable |
| realsense2_camera/CMakeLists.txt | Adds new header file to build configuration |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
Overall looks great! |
| private: | ||
| RSContextSingletonWrapper() = delete; | ||
| ~RSContextSingletonWrapper() = delete; | ||
| RSContextSingletonWrapper(const RSContextSingletonWrapper&) = delete; |
There was a problem hiding this comment.
Maybe we should add
RSContextSingletonWrapper(RSContextSingletonWrapper&&) = delete;
RSContextSingletonWrapper& operator=(RSContextSingletonWrapper&&) = delete;
ChatGPT:
Why deleting move is necessary
std::move would otherwise allow you to duplicate the singleton object.
Even if the moved-from object remains valid (depends on how the move is implemented), you’ve violated the invariant of “exactly one instance”.
It also makes ownership semantics unclear — should someone be allowed to “take” the singleton instance away? The answer is no.
That’s why both copy and move operations must be deleted.
There was a problem hiding this comment.
He also advise not to delete the Ctor/Dtor but to use default but not sure he is right there.
RSContextSingletonWrapper() = default; // must exist for `instance()` to work
~RSContextSingletonWrapper() = default; // private dtor prevents manual delete
};
There was a problem hiding this comment.
It is not correct because the way to access getInstance here is by calling the class method and not an object one. And I've validated it to be working.
I also think that if you are not able to create an object, then you probably are not able to std::move it... Right?
Maintain a single context when running in a composable node. This should help with having just a single realsense SDK and single source that access shared resources. Extra minor change: modified some header file includes to just reference the file instead of having a relative path.
82955f0 to
b4c82d4
Compare
Maintain a single context when running in a composable node. This should help with having just a single realsense SDK and single source that access shared resources.
Extra minor change: modified some header file includes to just reference the file instead of having a relative path.