Subscriber in a vector of objects

asked 2015-08-06 06:11:45 -0600

proprentnerl gravatar image


I have a class module_diagnostics_processor which subscribes to a certain topic and publishes certain diagnostics messages for the diagnostic aggregator to visualize them in the rqt console. There are about 70 diagnostic topics so I am using std::vector to simplify the code and found an interesting problem. When I use:

//diag_messages is a vector of structs with the message defintions
std::vector<module_diagnostics_processor*> module_processors;
for(int i = 0; i < diag_messages.size(); i++){
    module_processors.push_back(new module_diagnostics_processor(diag_messages[i]));    

everything works as expected and all 70 diagnostic topics are published for the diagnostic aggregator but when I don't use pointers:

std::vector<module_diagnostics_processor> module_processors;
for(int i = 0; i < diag_messages.size(); i++){

only the last element of the vector module_processors has it's callbacks called and publishes anything, altough rqt_graph shows all topics subscribed from my node.

i would really like to know where this difference in behavior comes from, so any input would be greatly appreciated.

edit retag flag offensive close merge delete