Hello:

I have several memory problems (my program starts to consume wild amounts of memory) in one of my applications and I'm thinking that it is because I don't properly delete certain items like structures.

For example I've got a Structure A wich contains a QVector of reals and a QVector of pointers. I enter a function that returns a Structure B which only contains 1 Pointer and 1 Value. So I do somemething like

Qt Code:
  1. StructB function(){
  2. StructA a;
  3. StructB b;
  4. //Do stuff that initializes a and b
  5. //In this stuff, there is a line that goes like b.pointer = a.vector[some_index];
  6. return b;
  7. }
To copy to clipboard, switch view to plain text mode 

How do I properly delete a? Or don't I have to?

Then I have a different situation. Where I have a PClass which is a parent class and two Descendants class calles ClassA and ClassB. Then I have a ClassC that uses all three in the following manner:

In ClassC.h there is a private variable declared like:
PClass *c;

In ClassC.cpp there is some code that says:
Qt Code:
  1. switch (some_variable){
  2. case 0:
  3. c = new ClassA();
  4. break;
  5. case 1:
  6. c = new ClassB();
  7. break;
  8. }
To copy to clipboard, switch view to plain text mode 

The user might then select something else. The ClassC object is NEVER destroyed until the end of the program. So if the user selects something else before the code above I wrote the following line:

delete c; //After this comes the switch.

Is this the right way to resolve this? Also Both Classes B and A have an extensive ammount of QVectors... Should I destroy (using QVector::clear() command) all the vectors in the Classe's Destructor?

Thanks for any help.