I have the following code. My program has a base class, that is c1. c2 and c3 are its derived classes. I also have a class called FilmDB(database), which stores all my films in one array, which name is films. In these if statements, i put the films into this array, depending on its type. (test condition). It works just fine, however f, cs, d will never be free, memory leaking comes. The problem is that i can not use delete [] within the if statement, because my program crashes, but i cant use it outside, because f, cs, d are not visible there as I see. Memtrace shows leak because of f, cs, and d.
if(test == "a"){
Class2 *c2 = new Class2(param1,param2,param3,param4);
films[i] = c2;
}
else if(test == "b"){
Class3 *c3 = new Class3(param1,param2,param3,param4);
films[i] = c3;
}
else {
Class1 *c1 = new Class1(param1,param2,param3);
films[i] = c1;
}
My FilmDB class has a virtual destructor which iterates the array and delete it, but it does not seem to solve my problem.
for(int i = 0; i < n; i++)
delete films[i];
EDIT FROM COMMENT:
I am not allowed to use std::vector
in my task