• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ - Destructors

Krakerjak

Senior member
Ok, i have this program.......

It consists of a base class (component), and 2 derived classes (resistor and capacitor).
I also have another class (circuit), which has a private element of an array of pointers to a component object.

My main program creates a circuit object. Then accepts input and using a makepart function will make a
resistor or a capacitor....or nothing depending on the input. I have this working for as many components as i want to enter.
When i want to print the netlist, i hit p and it will print each part (resistor or capacitor) and then ends.
Previously it would not run any of the destructors (was getting abnormal program termination message), and ran the base and either cap or resistor constructors at the beginning.

I made the circuit class destructor virtual, and tried again and it shows that it ran the circuit destructor according to the output, which is what i need.
BUT i also need the base class (component) destructor to run but it doesnt.
I tried making it virtual but nothing...... that will only allow me to run the derived class destructors i believe anyways.
Any ideas???

The reason i need thes destructors to run are for deallocating memory. I can post the class declarations if needed but i won't yet.
 
This is a sample output i get
(fields = : (component name) (x # of nodes) (node 1) (node 2) (node x) (value) ) :

Input component (ie. R1 2 1 0 10000): C1 2 1 3 10000
Base class constructor called
Derived class constructor called - Cap
Hit 'P' to print netlist, else continue entering components.

Input component (ie. C1 2 1 0 10000): R1 2 2 0 10000
Base class constructor called
Derived class constructor called - Resistor
Hit 'P' to print netlist, else continue entering components.

Input component (ie. R2 2 1 0 20000): C2 2 3 2 20000
Base class constructor called
Derived class constructor called - Cap
Hit 'P' to print netlist, else continue entering components.

Input component (ie. C2 2 1 0 20000): R2 2 1 0 20000
Base class constructor called
Derived class constructor called - Resistor
Hit 'P' to print netlist, else continue entering components.

R1 1 3 10K
C1 2 0 10nF
R2 3 2 22K
C2 1 0 22nF

Hit any key to exit.
Circuit class destructor called.
 
By definition, C++ virtual destructors are automatically called from most derived to the base class. So for example:

component * c = new resistor;
// ...
delete c;
// resistor::~resistor() is called
// then component::~component()
Based on the info you provided, I'll assume the problem is you do not iterate over the array of component pointers in your circuit destructor, and destroy each component instance. circuit does not derive from component, nor does establish a class hierarchy so its destructor does not necessarily need to be virtual (it's unclear why you stated this was a partial solution).

Standard disclaimer: I'm not a C++ developer; I just play one on AT sometimes. 😉
 
in the circuit destructor i had:

delete[] netlist;

changed it to

for(int f = ncomponents; f >= 1; f--)
delete netlist[f];

Now it will run the circuit destructor then the base destructor f number of times.

you do not iterate over the array of component pointers

That was it

 
Originally posted by: Krakerjak
in the circuit destructor i had:

delete[] netlist;

changed it to

for(int f = ncomponents; f >= 1; f--)
delete netlist[f];

You should actually do BOTH but in the following order:

// First delete each component in the for loop
for(int f = ncomponents; f >= 1; f--)
delete netlist[f];

// Now delete the memory allocated to store a list of pointers:
delete [] netlist;
 
ill try it...

One thing is though
The base class, or component, destructor wants to run each time i delete a netlist element (from circuit destructor).

it will go in this order:

cct class destructor
deleted component 4
base class destructor
cct class destructor
deleted component 3
base class destructor
cct class destructor
deleted component 2
base class destructor
cct class destructor
deleted component 1
base class destructor

The base destructor is the one i only need to run once, not delete the same 2 things x number of times.....or does it really matter?
 
Back
Top