0

This error is not easy to replicate since I am working with ROS 2 on Windows and the installation is not that simple but I think it is a Visual Studio issue so maybe you guys have a clue whats going on here.

I want to use ROS 2 in a Visual Studio 2019 project. To do this I did the following things:

  • C/C++ > Additional Include Directories = C:\dev\ros2_foxy\include
  • Linker > Additional Library Directories = C:\dev\ros2_foxy\Lib
  • Linker > Input > Additional Dependencies = all .lib files in
  • C:\dev\ros2_foxy\Lib Copied all .dll files to bin output

And I added the path C:/ros2_foxy/install/bin to my environment path.

Calling the ROS init function works fine so the connection to ROS should be fine. In ROS you use nodes to communicate. Your own nodes are classes which inherit from a "Node" parent class and are initialized with a node name (here "pub") like this:

class MinimalPublisher : public rclcpp::Node
{
public:
    MinimalPublisher(): Node("pub"){}
}

In the node.cpp of ROS the node class is defined like this:

Node::Node(
  const std::string & node_name,
  const NodeOptions & options)
: Node(node_name, "", options)
{
}

When I debug through the code node_name does not contain "pub" but a few random characters (they change every time), then "p" "u" "b" and "\0".

enter image description here

Because of this I get a "invalid node name" error. I tried to find out where exactly the random characters get added but to be honest I haven´t been able to find any earlier reference to node_name than this.

I'd be glad about any idea where the issue might be.

Edit: The definition of the node class is not that simple since it comes from ROS. All of this works fine when built with colcon and the command line, the issues only arise when using Visual Studio to build & run the code. But if you are interested you can find the documentation here and the implementation here.

My test code is from the "writing a publisher" example tutorial.

ItsMeTheBee
  • 353
  • 2
  • 20
  • here "pub" -> here "test" ?!? – 463035818_is_not_an_ai Mar 03 '21 at 13:31
  • 1
    Can you provide [MRE](https://stackoverflow.com/help/minimal-reproducible-example)? – Dan M. Mar 03 '21 at 13:36
  • sorry about that, I tried different strings and forgot to change it back – ItsMeTheBee Mar 03 '21 at 13:36
  • Please include the definition of the `Node` class and the three-argument Node constructor. – 0x5453 Mar 03 '21 at 13:37
  • Well a MRE would only work if you have ros installed on your system. If you have that you can use this: https://github.com/ItsMeTheBee/VisualStudioROS – ItsMeTheBee Mar 03 '21 at 13:37
  • Also, you say "I haven´t been able to find any earlier reference to node_name than this". Are you literally searching for the variable named `node_name`? Have you tried just walking through the stack and see what is calling the Node constructor and with what arguments? – Dan M. Mar 03 '21 at 13:38
  • I used the debugging tool to check what´s happening and step through every single operation and everything before that is just the iitialization of the node options. – ItsMeTheBee Mar 03 '21 at 13:44
  • Your spacing of the `&` reference modifier makes it look like the `&` boolean operator. Most style guides recommend attaching it to at least the type or name of the variable. – tadman Mar 03 '21 at 14:02
  • @ItsMeTheBee well something calls the Node constructor at some point (probably in the same place NodeOptions are finalized). It should be in the stack trace. I suspect the node_name (or whichever is passed as it) becomes dangling at some point. – Dan M. Mar 03 '21 at 14:14
  • Just a question for my own c++ culture: the "p" "u" "b" "\0" looks like the code is taking "pub" as a ```char```, doesn't it? Another question I have: The constructor of Node expects a reference value. If I use the value directly when calling the constructor, would this work? – ManyQuestions Mar 03 '21 at 14:57
  • @ManyQuestions what code? Debugger just prints the memory pointed to by the `node_name` string, but it's clearly in broken state by then (size has meaningless value). Also, what do you mean by "using value directly"? What value? What is direct? For `Node("test")` a temporary `std::string("test")` would be constructed and then the constructor would reference that (temporary can bind to _const_ reference). – Dan M. Mar 03 '21 at 15:09

0 Answers0