Recent

Author Topic: unreferenced object never freed?  (Read 3638 times)

rgh

  • New Member
  • *
  • Posts: 49
unreferenced object never freed?
« on: May 29, 2017, 11:56:59 am »
Here is the pascal equivalent of a style I've gotten used to in java.
Code: Pascal  [Select][+][-]
  1. TDog.create().bark;
With no garbage collection, do we simply not use this type of construction in object pascal?
Here is a complete program using this questionable style.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}        
  2.  
  3. program class_test0;
  4.  
  5. uses sysutils;
  6.  
  7. type
  8.   TDog = class
  9.   public
  10.     procedure bark;
  11.   end;
  12.  
  13. var
  14.   dog:TDog;
  15.  
  16. procedure TDog.bark;
  17. begin
  18.   writeln('woof!');
  19. end;
  20.  
  21. begin
  22.   dog := TDog.create();
  23.   dog.bark;
  24.   FreeAndNil(dog);
  25.   //memory never freed in pascal?
  26.   TDog.create().bark;
  27. end.

sky_khan

  • Guest
Re: unreferenced object never freed?
« Reply #1 on: May 29, 2017, 12:13:18 pm »
Yep. Last line will cause memory leak.  You may do something like below
Code: Pascal  [Select][+][-]
  1.   with TDog.Create do begin Bark;Free;end;
  2.  

but! it will get messy if you will run into name clashes or it has a potential to raise an exception in with block.


Eugene Loza

  • Hero Member
  • *****
  • Posts: 678
    • My games in Pascal
Re: unreferenced object never freed?
« Reply #2 on: May 29, 2017, 12:15:01 pm »
You're doing something very wrong.
I think you try to preform something like this:
Code: Pascal  [Select][+][-]
  1. type
  2.   TDog = class
  3.   public
  4.     class procedure bark;
  5.   end;
  6.  
  7. procedure TDog.bark;
  8. begin
  9.   writeln('woof!');
  10. end;
  11.  
  12. begin
  13.   TDog(nil).bark;
  14. end.
You can also try something exotic like COM-interfaces with their reference-counting (and therefore automatic freeing of classes)
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

rgh

  • New Member
  • *
  • Posts: 49
Re: unreferenced object never freed?
« Reply #3 on: May 29, 2017, 12:29:44 pm »
That's helpful.
Thanks.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: unreferenced object never freed?
« Reply #4 on: May 29, 2017, 12:51:16 pm »
To simulate java you have to do something like this

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}
  4. {$Interfaces COM} // not required since this is the default
  5. {$H+}
  6.  
  7. type
  8.   IDog = interface ['{055EE096-F887-467C-9B3C-491B42740844}']
  9.     procedure Bark;
  10.   end;
  11.  
  12.   TDog = class
  13.   public
  14.     procedure Bark;
  15.   end;
  16.  
  17.   TIDog = class(TInterfacedObject, IDog)
  18.   public
  19.     procedure Bark;
  20.   end;
  21.  
  22. procedure TIDog.Bark;
  23. begin
  24.   WriteLn('Woof! (via COM interface)');
  25. end;
  26.  
  27. procedure TDog.Bark;
  28. begin
  29.   WriteLn('Woof! (via standard class)');
  30. end;
  31.  
  32. var
  33.   dogI: IDog;
  34.   dog: TDog;
  35.  
  36. begin
  37.   dog := TDog.Create;
  38.   dogI:=TIDog.Create as IDog;
  39.   try
  40.     dog.Bark;
  41.     dogI.Bark;
  42.   finally
  43.     dog.Free;
  44.     //dogI is automatically freed
  45.   end;
  46.   WriteLn;
  47.   WriteLn('Press Enter to Quit');
  48.   ReadLn;
  49. end.

rgh

  • New Member
  • *
  • Posts: 49
Re: unreferenced object never freed?
« Reply #5 on: May 29, 2017, 01:46:25 pm »
To simulate java you have to do something like this

That's interesting code, thank you.

At line 38

Code: Pascal  [Select][+][-]
  1. dogI:=TIDog.Create as IDog;

is there any particular reason to cast to IDog?
As TIDog implements IDog, I thought the newly created instance is assignable to a variable of type IDog without any cast.




Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: unreferenced object never freed?
« Reply #6 on: May 29, 2017, 02:30:48 pm »
To simulate java you have to do something like this

That's interesting code, thank you.

At line 38

Code: Pascal  [Select][+][-]
  1. dogI:=TIDog.Create as IDog;

is there any particular reason to cast to IDog?

As TIDog implements IDog, I thought the newly created instance is assignable to a variable of type IDog without any cast.

You are exactly right.
Either way works fine..

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: unreferenced object never freed?
« Reply #7 on: May 29, 2017, 03:06:58 pm »
Code: Pascal  [Select][+][-]
  1. dogI:=TIDog.Create as IDog;

is there any particular reason to cast to IDog?
As TIDog implements IDog, I thought the newly created instance is assignable to a variable of type IDog without any cast.

The compiler, of course, knows that dogI is a COM interface since you have declared it as such. So the compiler does not need the "as" cast at all. However, I included it to make explicit to the reader that at the point of creation your dogI variable refers to an interface.
Because you can also write this:
Code: Pascal  [Select][+][-]
  1. ...
  2. var
  3.   dogI: IDog;
  4.   dog: TDog;
  5.   io: TInterfacedObject;
  6.  
  7. begin
  8.   dog := TDog.Create;
  9.   dogI:=TIDog.Create as IDog;
  10.   io:=TIDog.Create as TInterfacedObject;
  11.   try
  12.     dog.Bark;
  13.     dogI.Bark;
  14.     (io as TIDog).Bark;
  15.   finally
  16.     dog.Free;
  17.     //dogI is automatically freed
  18.     io.Free;
  19.   end;
  20.   WriteLn;
  21.   WriteLn('Press Enter to Quit');
  22.   ReadLn;
  23. end.

rgh

  • New Member
  • *
  • Posts: 49
Re: unreferenced object never freed?
« Reply #8 on: May 29, 2017, 07:54:10 pm »
Quote from: howardpc
However, I included it to make explicit to the reader
Thanks for the clarification.

 

TinyPortal © 2005-2018