vector of Ports leads to segfault

Dear all,

I'm trying to use a std::vector to create ports in my orocos component. I would like to use 1 or more (output) ports (with the same type) without re-compiling my component and add new code.

I found something about that in an (old) forum topic, so I'm using this code:

std::vector< OutputPort<geometry_msgs::PoseStamped>* > output  /*NOTE THE POINTER*/
 
for (int i = 0; i < NumberOfPorts; i++)
{
   output[i] = new OutputPort<geometry_msgs::PoseStamped>();
   this->ports()->addPort( "port", *output[i] );
}

... what I get is segmentation fault error.

There is something I'm doing wrong? Is it possible to use a vector of ports?

Thank you in advance!

Federico

[SOLVED]

I solved my issue.

std::vector needs to be resized before you can allocate something with new(). Only my fault...

So correct code will be:

std::vector< OutputPort<geometry_msgs::PoseStamped>* > output  /*NOTE THE POINTER*/
 
output.resize(NumberOfPorts);
 
for (int i = 0; i < NumberOfPorts; i++)
{
   output[i] = new OutputPort<geometry_msgs::PoseStamped>();
   this->ports()->addPort( "port", *output[i] );
}

Federico