Access Violation while retrieving Sql query text in 64-Bit

Hi,

I am facing the AV issue

  1. while returning a string from the function, where the function is producing a SQL Query.
  2. While trying to add values to object.

Tried with latest files.

In the First scenario , am trying to get the sql.query as an output from the function. (PS_Test)

In the second scenario, I was trying to add value to an object. When the add method of the object was called directly without interacting with Pascal Script, then the value was added successfully. But when we interact with Pascal Script, we are getting AV.

As both are resulting in AV, I mentioned in single topic. These scenarios works good with 32-bit.

Attaching a test case for better understanding.

For scenario 1:
Ps_test.7z (16.1 KB)

For scenario 2:
SampleObjTest.7z (16.6 KB)

Hi,

as I already said, use class helper like

type
  TMySQLQuery = class helper for TSQLQuery
  public
    function GetQueryText: String;
  end;


function TMySQLQuery.GetQueryText: String;
var
  i: Integer;
  s: string;
begin
  try
    Result := Self.SQL.Text;
    if Self.Params.Count = 0 then
      exit;
    for i := 0 to Self.Params.Count - 1 do
    begin
      if s > '' then
        s := s + ', ' + #13#10;
      s := s + ':' + Self.Params.Items[i].Name + ' [' +
        TruncateString(Self.Params.Items[i].Text, 100, True) + ']';
    end;
    Result := Result + #13#10 + s;
  except
    Result := Self.SQL.Text;
  end;
end;
RegisterMethod(@TSQLQuery.GetQueryText, 'GetQueryText');

ups_Import_SQL.pas (41.6 KB)


the second one also requires helper:

  TMyJSONobject = class helper for TJSONobject
    procedure AddInt(Key: UnicodeString; I: Int64);
    procedure AddString(Key: UnicodeString; S: UnicodeString);
  end;

procedure TMyJSONobject.AddInt(Key: UnicodeString; I: Int64);
begin
  Add(Key, I);
end;

procedure TMyJSONobject.AddString(Key: UnicodeString; S: UnicodeString);
begin
  Add(Key, S);
end;
    RegisterMethod(@TJSONobject.AddInt, 'AddInt');
    RegisterMethod(@TJSONobject.AddString, 'AddString');

Note: RTTI better works with UnicodeString in comparing with WideString. usage of WideString may cause errors at freeing RTTI’s TValue.

ups_Import_JSON.pas (16.0 KB)

I have tried these helpers with a mistake.
“RegisterMethod(@TMySQLQuery.GetQueryText, ‘GetQueryText’);”. This is what I did.

And without these helpers also it worked when I used the file shared by you long ago for a query. I would like to share the file with you.
InvokeCall.inc (9.6 KB)
uPSRuntime.pas (433.0 KB)

By using these files , the sql worked without helpers. There were differences b/w the latest files and the shared files. The files which I shared was once given by you for the same issue as a fix.

May I know for which cases I can use class helpers?

Hi,

of course, you can use attached files if it works OK for you.

the latest commit contains support for array of const which isn’t present in your attached files …


Class helpers is one way for using custom extension methods in x64 code.

1st Self parameter like

function TSQLQueryGetQueryText(Self: TSQLQuery): String;

isn’t supported anymore because it causes other issues for x64 platform:

however it can be solved via class helpers

  TMySQLQuery = class helper for TSQLQuery
  public
    function GetQueryText: String;
  end;

Thankyou very much. I have been crazy all these days regd. the files.

Hi,
We are still getting AV , though used class helpers as suggested.
Attached a sample project with comments on AV.

SamplearrTest.7z (19.0 KB)

Hi,

Looks like you missed my note:

if you replace

    property Field[const Key: widestring]: TJSONbase Read GetField
      Write SetField;

with

    property Field[const Key: UnicodeString]: TJSONbase Read GetField
      Write SetField;

and make changes in Json.pas and ups_Import_JSON.pas, error disappears

I have tested the things by changing to unicode in both files. When it changed to unicodestring, i got weird value in the key variable. Am getting the array.name into str1 variable. The key was “name”, but when i changed it to unicodestring , i got the key as “name%^” . So i reverted to original

The json.pas file didn’t support unicode string. It was a free library for delphi and they have mentioned clearly about widestrings.

Hi,

test things with my changes:
testcase.zip (18.5 KB)


you can introduce a new property in helper like

  property Field_W[const Key: UnicodeString]: TJSONbase Read GetField_W
      Write SetField_W;

Hi,

can we rename the self parameter and use the existing ones without class helpers? It’s just a thought

“function TSQLQueryGetQueryText(S_qry: TSQLQuery): String;”

Hi,

it won’t work with x64: it works a bit differently in comparing with x86 where Self can be passed as a 1st parameter.


it is impossible to recognize self object from 1st parameter where parameter name can be random, e.g.

  • function Format(const Format: string; const Args: array of const): string;
  • function TSQLQueryGetQueryText(random_name: TSQLQuery): String;