Access Violation while retrieving Sql query text in 64-Bit


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)


as I already said, use class helper like

  TMySQLQuery = class helper for TSQLQuery
    function GetQueryText: String;

function TMySQLQuery.GetQueryText: String;
  i: Integer;
  s: string;
    Result := Self.SQL.Text;
    if Self.Params.Count = 0 then
    for i := 0 to Self.Params.Count - 1 do
      if s > '' then
        s := s + ', ' + #13#10;
      s := s + ':' + Self.Params.Items[i].Name + ' [' +
        TruncateString(Self.Params.Items[i].Text, 100, True) + ']';
    Result := Result + #13#10 + s;
    Result := Self.SQL.Text;
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);

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

procedure TMyJSONobject.AddString(Key: UnicodeString; S: UnicodeString);
  Add(Key, S);
    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. (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?


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
    function GetQueryText: String;

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

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

SamplearrTest.7z (19.0 KB)


Looks like you missed my note:

if you replace

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


    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 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.


test things with my changes: (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;


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;”


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;