I am having some problems when the destructors of my program are called. I have these clases:
myModuleis the base class where the array of pointers is.myModule_inis a subclass that represents a module with input ports (there is amyModule_outand amyModule_inoutthat will not be showed here, since it is not necessary).moduleCis one of the input ports modules (moduleAandmoduleBaremyModule_outandmyModule_inout).
myModule:
class myModule
{
public:
myPort* secondary_ins[NUM];
myModule() {}
virtual ~myModule()
{
for(int i=0; i<NUM; i++)
{
if(secondary_ins[i])
delete secondary_ins[i];
}
}
virtual void connect(myModule &m) = 0;
};
myModule_in:
class myModule_in : public myModule
{
public:
virtual ~myModule_in() {}
virtual void connect(myModule &m)
{
secondary_ins[ins] = new myPort();
...
}
};
moduleC:
class moduleC : public myModule_in
{
public:
moduleC();
~moduleC() {}
void connect_modules() {...}
};
main:
int main(...)
{
moduleA mA;
moduleB mB;
moduleC mC;
...
mA.connect(mB);
mB.connect(mC);
...
return 0;
}
This code compiles correctly and works fine, until the end of execution, where I get a segmentation fault at: delete secondary_ins[i]; being called from return 0;. The strange thing is that the other modules' destructor are also called and do not have any problem. Maybe there is something wrong with the handling of array of pointers inherited from a base class or something? Any ideas?
Thanks :)