Ask Your Question

ConstPtr NULL with waitForMessage

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

Borob gravatar image


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>(
            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


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 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

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

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


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 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



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

Seen: 885 times

Last updated: Mar 23 '16