0
$\begingroup$

Rosanswers logo

Hello, I do have troubles understanding the concept of messages regarding pointers. In this tutorial at the chatterCallback the argument msg is from type ::ConstPtr. What exactly does this ::ConstPtr mean? why is it not just const st_msgs::String? Also I saw that sometimes instead of String::ConstPtr StringConstPtr is used. Whats the difference here. As I realy would like to understand this concept with Message::ConstPtr, Message::Ptr, MessagePtr, MessageConstPtr I look for some kind of tutorial but can't find anything.

To give some example what my current problem is, here some code that does compile without troubles, but as soon as I run the code the node dies.

nav_msgs::OccupancyGrid::Ptr occGrid; [ORIGINAL = FALSE]
nav_msgs::OccupancyGrid::Ptr occGrid(new nav_msgs::OccupancyGrid());  [EDIT = WORKING]
for (int i=0; i<100; i++) {
  occGrid->data.push_back(0);
}
/* fill header and info field */
gridmap_2d::GridMap2DPtr map( new gridmap_2d::GridMap2D(occGrid));
/* use map as function argument */

As I realy would like to understand this ::Ptr issue a link to some tutorials or useful readings would be great!

Thanks Johannes


Originally posted by Johannes on ROS Answers with karma: 75 on 2013-09-12

Post score: 0

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

The Ptr/ConstPtr are defined by the ROS message generation and are boost shared pointers. AFAIK Msg::Ptr and MsgPtr are exactly the same thing, i.e. it's just a typedef.

As to your actual code, you only define a pointer occGrid, but never fill that with the actual nav_msgs data structure, so it points to nothing.


Originally posted by dornhege with karma: 31395 on 2013-09-12

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by Johannes on 2013-09-12:
OK, at least this error is removed. At least I think I managed to did it right now. Thx

$\endgroup$
0
$\begingroup$

Rosanswers logo

Going on a limp here because I sort of learned cpp by learning by doing, so im looking forward to someone replying who knows his cpp :). But until then:

I think the rough idea here is that Ptr stands for pointer. Why not use standard cpp pointers? you don't have to clean up after ptrs. I suppose your program crashes because you're calling methods of the object which your pointer is pointer to which is "not initialized". Can do that by assigning the pointer to an object, either at creation or at any point before calling methods.

I think they are called smart pointers, you might find more information when googling for that.


Originally posted by jodafo with karma: 365 on 2013-09-12

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by Johannes on 2013-09-12:
Smart pointers is a good term to google for. I will have a look. Googling for ::Ptr and ::ConstPtr was not realy successful. THX

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.