-3

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

granmirupa
  • 2,780
  • 16
  • 27
  • 3
    You should use _std::vector_ and avoid the use of _delete_ in C++ – granmirupa May 07 '17 at 13:17
  • provide a proper question. We can;t understand from this. – user2736738 May 07 '17 at 13:25
  • Do `Csaladi`, `Dokumentum` and `Film` have common base class? Does that class has virtual destructor? Is `filmcount` a correct value? – Andrey Turkin May 07 '17 at 13:29
  • Film is the base class, Csaladi and Dokumentum are the derived ones. I tried to edit my code to make it more understandable. –  May 07 '17 at 13:32
  • Run your code in a debugger and find the crash. There is no earthly way we can proffer anything but wags (wild-arse-guesses) that we could offer reasons for why that happens, as you have nothing even remotely resembling a [minimum, **complete**, verifiable example](https://stackoverflow.com/help/mcve) that reproduces your problem. – WhozCraig May 07 '17 at 13:44

2 Answers2

1

Really I don't understand why people use C++ unless they use features of it. Use vector and delegate the task of destruction automatically.

Use vector and read the documentation before you do that.

In case of vector..you can do like this

vector<classname> v;
v.push_back( class's object);
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • I am not allowed to use vector in my task, it is something i forgot to mention, sorry. –  May 07 '17 at 13:19
  • 2
    @TamasHargitai edit your question. We can't guess what you want to do. – granmirupa May 07 '17 at 13:22
  • There would have to be a lot more involved than just pushing into a `std::vector` with this, as the OP has a polymorphic derivation hierarchy, and this solution would regress to [object slicing](https://stackoverflow.com/questions/274626/what-is-object-slicing). – WhozCraig May 07 '17 at 13:33
  • I dont know all these name but yes here in this case...an properly hierarchical class would do the trick. @WhozCraig – user2736738 May 07 '17 at 13:34
  • @WhozCraig.: If possible you can suggest me to modify or you can m,odify or wrrite a new answer...anyway it will help me answer this half specified question. – user2736738 May 07 '17 at 13:36
  • @coderredoc There really isn't much point to do so, as the OP has made this somewhat irrelevant once they updated their question with the no-vector clause. They can't use `std::vector`, and certainly can't use what would be required to make this a proper [RAII](http://en.cppreference.com/w/cpp/language/raii) solution (a `std::vector` of some smart pointer type (ex: `std::unique_ptr`) to fulfill the polymorphic requirements. – WhozCraig May 07 '17 at 13:39
  • @WhozCraig.: Yes it doesn't meet the purpose of OP but atleast would serve the purpose of future users...rest is what you decide...`unique_ptr` is the best the language can offer I would say...hope OP reads these discussion and read accordingly. – user2736738 May 07 '17 at 13:41
0

Are you reusing the index of films[i] at all?

In that case you need to delete the memory there before you add a newly allocated object:

if(test == "a"){
            Class2 *c2 = new Class2(param1,param2,param3,param4);
            delete films[i];
            films[i] = c2;
        }


        else if(test == "b"){
            Class3 *c3 = new Class3(param1,param2,param3,param4);
            delete films[i];
            films[i] = c3;
        }


        else {
            Class1 *c1 = new Class1(param1,param2,param3);
            delete films[i];
            films[i] = c1;
        }

For this to work however, you need for films[i] to be either NULL or a valid memory location. If films[i] is not initialized and you call delete upon it it will crash. You need to handle that on the parts of the program that I am not seeing at the moment.

Also, it is best to debug and make sure your destructor is being called.

didiz
  • 1,069
  • 13
  • 26