ConstPtr NULL with waitForMessage
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.
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.