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

ConstPtr NULL with waitForMessage

asked 2016-03-23 00:34:32 -0500

Borob gravatar image

Hello.

I have a function which is supposed to just get one message from a topic, the function looks like this:

std::vector<my_msgs::Person> getPersonList()
{   
    ros::NodeHandle n;
    ros::Duration four_seconds(4.0);

    my_msgs::PersonListConstPtr person_list = ros::topic::waitForMessage<my_msgs::PersonList>(
            "/my_world/persons_available",
            n, four_seconds);
    std::vector<my_msgs::Person> person_vector;
    person_vector = person_list->persons;
    return person_vector;
}

Whereas PersonList.msg is:

Person[] persons

And Person.msg something like:

string name
int age
...

Now when I call the function, the behavior is rather erratic. Sometimes I get the correct results for the first call(s) but after some time I get a Assertion 'px != 0' failed. Googling around told me that the shared_ptr PersonListConstPtr is NULL. And indeed when I do:

if(person_list != NULL)
        person_vector = person_list->persons;

My program is not crashing, but instead nothing happens, because apparently my pointer is always NULL.

Now, the topic is correct, it is published and everything. I have the suspicion that the original pointer is still active and that I can't get hold on it anymore after returning. Can someone help me what to do? I am not very familliar with those shared_ptr. I already tried to get() the pointer but got a segmentation fault.

edit retag flag offensive close merge delete

Comments

1

Strangely, it worked when I use the function that does not require NodeHandle and Duration. If someone comes up with a solution with a timeout (becuase it is clearly not waiting the time I set), I will accept the answer.

Borob gravatar image Borob  ( 2016-03-23 11:02:30 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-03-23 01:46:32 -0500

al-dev gravatar image

Could it be that your pointer person_list is simply set to NULL when waitForMessage times out after the duration you specified as parameter (4 sec)? See this thread.

edit flag offensive delete link more

Comments

I now set the timeout to 10 seconds and it definitely returns earlier with a NULL object. Whereas the first call always works.

Borob gravatar image Borob  ( 2016-03-23 10:56:22 -0500 )edit

Same issue here. Sometimes it returns an empty pointer. Have you found the source of this problem?

niedzviedz gravatar image niedzviedz  ( 2021-03-11 12:32:24 -0500 )edit

Question Tools

4 followers

Stats

Asked: 2016-03-23 00:34:32 -0500

Seen: 1,434 times

Last updated: Mar 23 '16