ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Message vs. Message::Ptr

asked 2013-09-12 00:35:32 -0500

Johannes gravatar image

updated 2013-09-12 04:20:29 -0500

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

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2013-09-12 01:20:42 -0500

dornhege gravatar image

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.

edit flag offensive delete link more

Comments

OK, at least this error is removed. At least I think I managed to did it right now. Thx

Johannes gravatar image Johannes  ( 2013-09-12 04:21:22 -0500 )edit
1

answered 2013-09-12 01:14:51 -0500

jodafo gravatar image

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.

edit flag offensive delete link more

Comments

Smart pointers is a good term to google for. I will have a look. Googling for ::Ptr and ::ConstPtr was not realy successful. THX

Johannes gravatar image Johannes  ( 2013-09-12 04:22:37 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2013-09-12 00:35:32 -0500

Seen: 5,000 times

Last updated: Sep 12 '13