Skip to content

Create a singleton wrapper to rs2::context - #3430

Merged
Nir-Az merged 1 commit into
realsenseai:ros2-developmentfrom
Gilaadb:singleton-context-wrapper
Aug 31, 2025
Merged

Nir-Az merged 1 commit into
realsenseai:ros2-developmentfrom
Gilaadb:singleton-context-wrapper

Conversation

@Gilaadb

@Gilaadb Gilaadb commented Aug 27, 2025

Copy link
Copy Markdown
Contributor

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.

@Gilaadb
Gilaadb force-pushed the singleton-context-wrapper branch from a891ab3 to 82955f0 Compare August 27, 2025 12:16
@Nir-Az
Nir-Az requested review from Nir-Az and Copilot August 27, 2025 12:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Nir-Az

Nir-Az commented Aug 28, 2025

Copy link
Copy Markdown
Collaborator

Overall looks great!

private:
RSContextSingletonWrapper() = delete;
~RSContextSingletonWrapper() = delete;
RSContextSingletonWrapper(const RSContextSingletonWrapper&) = delete;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
};

@Gilaadb Gilaadb Aug 28, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@Gilaadb
Gilaadb force-pushed the singleton-context-wrapper branch from 82955f0 to b4c82d4 Compare August 28, 2025 13:26
@Nir-Az
Nir-Az merged commit 14f5e9e into realsenseai:ros2-development Aug 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants