#1 2018-12-26 19:26:45

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Destroy method issue on DWScript 1.x and 2.x branch

I spent hours debugging an issue with mORMot classes with SMS 3.x I couldn't detect why it couldn't work like in the previous version 2.2x. I think the error comes from the /destroy method.

In previous versions, SMS 1.2 | 2.0 | 2.1 | 2.2 | 2.2.2, the method Destroy emitted by transpiler is

var TObject={
	$ClassName: "TObject",
	$Parent: null,
	ClassName: function (s) { return s.$ClassName },
	ClassType: function (s) { return s },
	ClassParent: function (s) { return s.$Parent },
	$Init: function () {},
	Create: function (s) { return s },
	Destroy: function (s) { for (var prop in s) if (s.hasOwnProperty(prop)) delete s.prop },
	Destroy$: function(s) { return s.ClassType.Destroy(s) },
	Free: function (s) { if (s!==null) s.ClassType.Destroy(s) }
}

Note this code would just delete the property named 'prop' rather than the actually properties of the object itself, which in turns meant references in a "freed" object could not be garbage collected until the freed object got out of the scope.

The Destroy method emitted by SMS 3.x is correct:

var TObject={
    (...)
    Destroy: function (s) { for (var prop in s) if (s.hasOwnProperty(prop)) delete s[prop] },
    Destroy$: function(s) { return s.ClassType.Destroy(s) },
    (...)
}

The following minimal snippet of code worked in previous SMS 2.0 | 2.1 | 2.2, but not longer works as expected in version 3x. See the client.Free automatically calls the destructor.

var
  model: TSQLModel;
  people: TSQLRecordPeople;
  client: TSQLRestClientHTTP;
begin
  model := GetModel;
  model.GetTableIndexExisting(TSQLRecordPeople);
  people := new TSQLRecordPeople;

     client := TSQLRestClientHTTP.Create('127.0.0.1',888,model,false);
     client.Connect(
      lambda

        if client.ServerTimeStamp=0 then
          ShowMessage('Impossible to retrieve server time stamp') else
          writeln('ServerTimeStamp='+IntToStr(client.ServerTimeStamp));
        if not client.SetUser(TSQLRestServerAuthenticationDefault,'User','synopse') then
          ShowMessage('Authentication Error');
        writeln('Safely connected with SessionID='+IntToStr(client.Authentication.SessionID));
        people := TSQLRecordPeople.Create(client,1); // blocking request
        assert(people.ID=1);
        writeln('Disconnect from server');
        client.Free;
      end,
      lambda
        ShowMessage('Impossible to connect to the server');
      end);

Note: mORMot examples 27 and 29 should not work with SMS 3.x out of box.

Offline

#2 2018-12-27 08:41:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Destroy method issue on DWScript 1.x and 2.x branch

We don't support SMS 3.x yet...

Offline

#3 2018-12-27 14:27:32

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: Destroy method issue on DWScript 1.x and 2.x branch

It compiles and works in both SMS 2.2.2.04694 (last WinXP compatible) and SMS 3.1.0.80 (development version)!

The updated examples 27 and 29 --> https://www.dropbox.com/s/uonih6izjpiw7 … x.rar?dl=0

The current mORMot branch, there is an issue at the example 29. When you open the project, there's an ugly message: "Error while loading project file with the message: WebForm.pas doesn't exist". You can not open the project 29, because there's an bug in the SMS 2.x (Make Project/Item external) this issue, you can miss units on the project.

Nowadays, the synchronous methods are deprecated is most modern browsers, it shows a info message:
"[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/."

Yeah, that "false" makes the request synchronous (i.g. request.open('GET', '/bar/foo.txt', false)). Anyway, both synchronous and asynchronous methods work, depending on use case. For instance, you can invoke a method to calculate both in syn or async ways. When you call a method starting with underscore _*() methods means you are invoking as sync way, this should be avoid in these days.

Offline

Board footer

Powered by FluxBB