Recent

Author Topic: [Solved] Destructor is giving me error  (Read 467 times)

nikel

  • Sr. Member
  • ****
  • Posts: 282
[Solved] Destructor is giving me error
« on: June 11, 2025, 04:01:29 am »
Hello, I'm trying to delete a stringlist in my child class. I attached the screenshot of the error. Here's my code:

Code: Pascal  [Select][+][-]
  1. type
  2.  
  3.   { TMain }
  4.  
  5.   TMain = class
  6.   protected
  7.     procedure DoRun;
  8.   public
  9.     constructor Create();
  10.     destructor Destroy;
  11.     procedure WriteHelp(Ver: shortstring); virtual;
  12. ...
  13.  
  14. type
  15.   TParameterHandler = class(TMain)
  16.   private
  17.     ListOfParameters    : TStringList;
  18.   public
  19.     constructor Create();
  20.     destructor Destroy;
  21.  
  22.     function CreateIndex(Directory: string): Boolean;
  23. ...
  24.  
  25. destructor TParameterHandler.Destroy;
  26. begin // This line is giving me error
  27.   inherited Destroy;
  28.   ListOfParameters:=nil;
  29.   ListOfParameters.Free;
  30. end;

How can I fix this error?
« Last Edit: June 11, 2025, 05:15:19 am by nikel »

egsuh

  • Hero Member
  • *****
  • Posts: 1817
Re: Destructor is giving me error
« Reply #1 on: June 11, 2025, 04:57:27 am »
You shoulld add "override" to all destroy destructors, as all classes are descendant of TObject.
And you should order in following order

Code: Pascal  [Select][+][-]
  1. destructor TParameterHandler.Destroy;
  2. begin // This line is giving me error
  3.   ListOfParameters.Free;
  4.   ListOfParameters:=nil;  // this is not necessary
  5.  
  6.   inherited Destroy;
  7.  
  8. end;

nikel

  • Sr. Member
  • ****
  • Posts: 282
Re: Destructor is giving me error
« Reply #2 on: June 11, 2025, 05:14:58 am »
That worked fine. Thank you for your help.

egsuh

  • Hero Member
  • *****
  • Posts: 1817
Re: [Solved] Destructor is giving me error
« Reply #3 on: June 11, 2025, 05:54:47 am »
Good to hear that the issue has been solved. To explain a little more,


Code: Pascal  [Select][+][-]
  1. destructor TParameterHandler.Destroy;
  2. begin // This line is giving me error
  3.   inherited Destroy;    //------ > this destroys object itself, and so nothing is left.
  4.   ListOfParameters:=nil;  
  5.   ListOfParameters.Free;   // --> even though "inherited destroy" had not been executed, you have set ListOfParameters to nil, and you are calling nil.Free, which is error.
  6. end;

 

TinyPortal © 2005-2018