Recent

Author Topic: trouble with dispose()  (Read 480 times)

lazer

  • Full Member
  • ***
  • Posts: 201
trouble with dispose()
« on: October 24, 2022, 01:23:26 am »
Hi,

I'm having trouble allocating and freeing an instance of a class I've defined.


Code: Pascal  [Select][+][-]
  1.   Ppack=^Tpack;
  2.   Tpack= class
  3.      public
  4.        constructor init(owner:TForm;packreq:integer);
  5.        destructor done; virtual;
  6.  
  7.   end;
  8.  
  9.  
  10. ....
  11.  
  12.  
  13. procedure mainWin.load;
  14. begin
  15. var
  16.   oldpack:Tpack;
  17.  
  18. begin  {loadpack}
  19.   oldpack:=pack;
  20.   pack:=Tpack.init(self,packreg);
  21.  
  22.   if pack=NIL then pack:=oldpack
  23.   else
  24.   begin
  25.     if (oldpack<>NIL) then dispose(oldpack);          /// no longer calls done() ???
  26.  
  27.  
That fails to compile the dispose() call with ; Error , pointer type expected.

Now I could init a Ppack and then use pack^ everywhere and dispose() that but I though class variables were a pointer anyway.

It's probalby just needs a cast or something but I don't like guessing and messing with points.  Can someone point to how to use this? I don't seem to find anything helpful on dispose.

Thanks.

dseligo

  • Hero Member
  • *****
  • Posts: 813
Re: trouble with dispose()
« Reply #1 on: October 24, 2022, 02:47:59 am »
Maybe
Code: Pascal  [Select][+][-]
  1. oldpack.Free;
?

lazer

  • Full Member
  • ***
  • Posts: 201
Re: trouble with dispose()
« Reply #2 on: October 24, 2022, 03:12:22 am »
Thanks .

I did get it to compile and run by using a pointer varaible  like this:

Code: Pascal  [Select][+][-]
  1. var
  2.   oldpack:Ppack;
  3. ....
  4.  
  5.   oldpack:=@pack;
  6. ......
  7.  
  8.     if (oldpack<>NIL) then dispose(oldpack);          /// no longer calls done() ???
  9.  

I don't know whether that is correct usage .

beria

  • Jr. Member
  • **
  • Posts: 70
Re: trouble with dispose()
« Reply #3 on: October 24, 2022, 05:11:21 am »
Thanks .

I did get it to compile and run by using a pointer varaible  like this:

Code: Pascal  [Select][+][-]
  1. var
  2.   oldpack:Ppack;
  3. ....
  4.  
  5.   oldpack:=@pack;
  6. ......
  7.  
  8.     if (oldpack<>NIL) then dispose(oldpack);          /// no longer calls done() ???
  9.  

I don't know whether that is correct usage .
https://wiki.freepascal.org/FreeAndNil

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: trouble with dispose()
« Reply #4 on: October 24, 2022, 07:05:38 am »
@lazer:
you have mix old-style pascal object with class. That no good and are differ.

To have better understand class maybe can read "introduction to modern pascal" topic classes: https://castle-engine.io/modern_pascal#_classes

PascalDragon

  • Hero Member
  • *****
  • Posts: 4759
  • Compiler Developer
Re: trouble with dispose()
« Reply #5 on: October 24, 2022, 07:21:33 am »
I don't know whether that is correct usage .

No, it's not. The correct usage is oldpack.Free with oldpack being declared as Tpack. Also you need to name your destructor Destroy and mark it with override instead of virtual so that it's called correctly.

lazer

  • Full Member
  • ***
  • Posts: 201
Re: trouble with dispose()
« Reply #6 on: October 24, 2022, 09:18:51 am »
@lazer:
you have mix old-style pascal object with class. That no good and are differ.

To have better understand class maybe can read "introduction to modern pascal" topic classes: https://castle-engine.io/modern_pascal#_classes

Thanks, it's a long time since I did any serious work in Pascal. While fpc attempts to maintain backwards compatibility, it's probably a good idea to use one or the other.

Thanks also to PascalDragon, a quick fix of how to do this correctly is a great help to get this running cleanly. 

MarkMLl

  • Hero Member
  • *****
  • Posts: 5577
Re: trouble with dispose()
« Reply #7 on: October 24, 2022, 09:28:05 am »
Thanks, it's a long time since I did any serious work in Pascal. While fpc attempts to maintain backwards compatibility, it's probably a good idea to use one or the other.

Thanks also to PascalDragon, a quick fix of how to do this correctly is a great help to get this running cleanly.

I'm afraid there's no "probably" about it... and PascalDragon was being uncharacteristically subdued :-)

Somebody prepared this document a few months ago looking at the difference between classes, objects, advanced records etc., which I think is worth reading.

https://forum.lazarus.freepascal.org/index.php?action=dlattach;topic=57040.0;attach=46085

https://github.com/zsoltszakaly/OOPstructuresinpascal/blob/main/structures%20in%20pascal.odt

I think that the .odt version is slightly newer, and the author is interested in any clarifications that the community can provide.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018