[fpc-pascal] ppcjvm issues

Dmitry Boyarintsev skalogryz.lists at gmail.com
Fri Jan 27 15:36:07 CET 2017


On Tue, Jan 10, 2017 at 6:14 AM, Michael Schnell <mschnell at lumino.de> wrote:

>
> If destroying an object is not necessary, the class should provide a dummy
> Free procedure. So the application programmer always can/should use Free.
>
> Why dummy? if it should be like this

procedure TObject.Free;
begin
  if Self<>nil then Self:=nil;
end;

Destroying object is not necessary, but dereferencing is.
If the code keeps the reference to an object, it would not be collected.

For example. In pascal:
var
  b : TSomeClass;
begin
  b := TSomeClass.Create; // allocated
  ..
  b.Free;  // freed (b becomes an invalid pointer)
  ...
end.

JVM with dummy Free
var
  b : TSomeClass;
begin
  b := TSomeClass.Create; // allocated
  ..
  b.Free;  // does nothing (b remains a valid pointer)
  ...
end.// a might be freed after leaving  the scope of visibility

JVM with nil-ing Free.
var
  b : TSomeClass;
begin
  b := TSomeClass.Create; // allocated
  ..
  b.Free;  // derefering a, (a, become nil)

  ... // a might be Freed here somewhere, whenever gc occurs
end.

thanks,
Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20170127/2d01687d/attachment.html>


More information about the fpc-pascal mailing list