Dereferencing an object handed into a thread by Pointer lazarus / 21-General ---------------------------------------------------------------------- RedOctober · 2024-11-28 20:57 Lazarus 3.4 (rev lazarus_3_4) FPC 3.2.2 x86_64-win64-win32/win64 OS: Windows Server 2016 Component in use:  Brook 5 Framework Background:  I am trying to access parameter values that I have handed into an isolated thread of the Brook 5 ARequest.Isolate() function.  I have included the pertinent sections of code here, and my question is at the end. // Type Declaration: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TISO_Parms = class    seg_txt_rbi: String;    req_lst, rsp_lst, fli_lst, flo_lst: TStringList;    sys_err: Boolean;    sys_err_txt: String;    db_data_rqrd: String;    dmISO: TDataModule;  end;  // Prepare an instance of ISO_Parms to hold all our stuff Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ISO_Parms := TISO_Parms.Create;       ISO_Parms.seg_txt_rbi := seg_txt_rbi;     ISO_Parms.req_lst := TStringList.Create;      ISO_Parms.rsp_lst := TStringList.Create;      ISO_Parms.fli_lst := TStringList.Create;    ISO_Parms.flo_lst := TStringList.Create;       ISO_Parms.req_lst.AddStrings(req_lst);      ISO_Parms.rsp_lst.AddStrings(rsp_lst);      ISO_Parms.fli_lst.AddStrings(fli_lst);      ISO_Parms.flo_lst.AddStrings(flo_lst);       ISO_Parms.sys_err := sys_err;      ISO_Parms.sys_err_txt := sys_err_txt;      ISO_Parms.db_data_rqrd := db_data_rqrd;      ISO_Parms.dmISO := nil;  // I'll create this later, inside the thread  // Call the "Isolator" procedure Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ARequest.Isolate(@GetAllDashItems_ISO);  // Isolated procedure I am having trouble with Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetAllDashItems_ISO(ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse; AUserData: Pointer);begin  GetAllDashItemsIsolated(ARequest, AResponse,    AUserData^.ISO_Parms.seg_txt_rbi,  // <-- Getting error here:  Illegal Qualifier // ... of course, all the lines below have the same problem as the line aboveAUserData.req_lst, AUserData.rsp_lst, AUserData.fli_lst, AUserData.flo_lst,    AUserData.sys_err, AUserData.sys_err_txt, AUserData.db_data_rqrd,    AUserData.dmISO);end;  Question: What is the proper syntax for dereferencing (accessing) the values in the parameters contained in my ISO_Parms object.  I have built isolated threads in the Brook 5 Framework before, with success, but I have never before included anything in the AUserData parameter. Thanks in advance for any help you can provide. ---------------------------------------------------------------------- TRon · 2024-11-28 21:05 And what is passed as AUserdata then ? That generic pointer should be cast to whatever was passed there. fwiw: Isolate has a second parameter for the userdata (and the code shown does not seem to use it). ---------------------------------------------------------------------- MarkMLl · 2024-11-28 21:12 A question like this with isolated fragments quite simply doesn't give us enough to work with, but first apply an assertion that the parameter is the specified class, and then cast it using as. MarkMLl ---------------------------------------------------------------------- RedOctober · 2024-11-28 21:48 Hi Mark and Ron.  I'm making progress, based on your responses, but I'm not quite there yet. Here is the pertinent source code from Brook: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      procedure Isolate(AProc: TBrookHTTPRequestIsolatedProc;      AUserData: Pointer = nil);{$IFNDEF FPC}overload;{$ENDIF}virtual;     { Isolates a request from the main event loop to an own dedicated thread,      bringing it back when the request finishes.      @param(AProc[in] Anonymous Procedure to handle requests and responses      isolated from the main event loop.)      @param(AUserData[in] User-defined data.) }    procedure Isolate(const AProc: TBrookHTTPRequestIsolatedAnonymousProc;      AUserData: Pointer = nil); overload; virtual;    { TBrookHTTPReqIsolatedProcHolder }   TBrookHTTPReqIsolatedProcHolder = class  private    FProc: T;    FUserData: Pointer;  public    constructor Create(const AProc: T; AUserData: Pointer);    property Proc: T read FProc;    property UserData: Pointer read FUserData;  end; { TBrookHTTPReqIsolatedProcHolder } constructor TBrookHTTPReqIsolatedProcHolder.Create(const AProc: T;  AUserData: Pointer);begin  inherited Create;  FProc := AProc;  FUserData := AUserData;end; procedure TBrookHTTPRequest.Isolate(AProc: TBrookHTTPRequestIsolatedProc;  AUserData: Pointer);var  VHolder: TBrookHTTPReqIsolatedProcHolder;begin  SgLib.Check;  SetIsIsolated(True);  VHolder := TBrookHTTPReqIsolatedProcHolder<    TBrookHTTPRequestIsolatedProc>.Create(AProc, AUserData);  try    SgLib.CheckLastError(sg_httpreq_isolate(FHandle,      DoRequestIsolatedProcCallback, VHolder));  except    VHolder.Free;    raise;  end;end;   I modified my code a bit, as shown below.  Now it compiles but the variables contain nothing.  Still stuck. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetAllDashItems_ISO(ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse; AUserData: Pointer);var s: String;begin  // The section below compiles, but the thing cast as TISO_Parms contain nothing, and give a memory access error  s := TISO_Parms(AUserData).seg_txt_rbi;  ShowMessage('s: ' + s);  // This just displays "s: " with nothing after it.   // The section below compiles, but the things cast as TISO_Parms contain nothing, and give a memory access error  GetAllDashItemsIsolated(ARequest, AResponse,  TISO_Parms(AUserData).seg_txt_rbi,    TISO_Parms(AUserData).req_lst,  TISO_Parms(AUserData).rsp_lst,    TISO_Parms(AUserData).fli_lst,    TISO_Parms(AUserData).flo_lst,    TISO_Parms(AUserData).sys_err,    TISO_Parms(AUserData).sys_err_txt,    TISO_Parms(AUserData).db_data_rqrd);end;  I also changed how I call it: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       ARequest.Isolate(@GetAllDashItems_ISO, @ISO_Parms); ---------------------------------------------------------------------- RedOctober · 2024-11-28 21:54 I think I've got it now.  I just had to change this last thing, using the modified code shown above: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       ARequest.Isolate(@GetAllDashItems_ISO, ISO_Parms);  Seems the last parameter must not be a pointer, only the first one. ---------------------------------------------------------------------- TRon · 2024-11-28 21:58 Quote from: RedOctober on November 28, 2024, 10:54:00 pmI think I've got it now.  I just had to change this last thing, using the modified code shown above: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i       ARequest.Isolate(@GetAllDashItems_ISO, ISO_Parms);  Seems the last parameter must not be a pointer, only the first one. Both are pointers but how to indicate depends on the compiler mode (because first pointer is a pointer to a function (technically the address of the function) ). The second pointer can be anything you want to, in your case a pointer (technically in this case the address of the class instance) to a class. PS: it might perhaps help to have a look at the help of the isolate method. ---------------------------------------------------------------------- cdbc · 2024-11-28 22:27 Hi This seems correct:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      ARequest.Isolate(@GetAllDashItems_ISO, ISO_Parms);...and this usage Needs a bit:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetAllDashItems_ISO(ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse; AUserData: Pointer);var   liso: TISO_Parms; ///// (l)ocal(iso) var /////begin///// here typecast to a (l)ocal(iso) variable ////////// reason for the 'FUserData' we find in: ////////// 'TBrookHTTPReqIsolatedProcHolder.Create' //////  liso:= TISO_Parms(ARequest.FUserData); /// copied in isolate() ///// below we then use the local varible /////  GetAllDashItemsIsolated(ARequest, AResponse,    liso.seg_txt_rbi,    liso.req_lst,    liso.rsp_lst,    liso.fli_lst,    liso.flo_lst,    liso.sys_err,    liso.sys_err_txt,    liso.db_data_rqrd);///// behind the scenes a TObject is just an elaborate pointer /////end;I think that should do it... Regards Benny ---------------------------------------------------------------------- cdbc · 2024-11-28 22:41 Hi I changed my post a few times after, beware... Regards Benny ---------------------------------------------------------------------- RedOctober · 2024-11-28 22:44 Hi cdbc, Should I be .Free ing the local instance of liso, like this... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   ...   liso:= TISO_Parms(ARequest.FUserData);   GetAllDashItemsIsolated(ARequest, AResponse,    liso.seg_txt_rbi,    liso.req_lst,    liso.rsp_lst,    liso.fli_lst,    liso.flo_lst,    liso.sys_err,    liso.sys_err_txt,    liso.db_data_rqrd);     liso.Free; ...   or will the isolated thread free itself, and everything in it, when it completes? ---------------------------------------------------------------------- cdbc · 2024-11-28 22:48 Hi H*LL NO, no freeing, you are just borrowing it!!!! The freeing takes place outside of the thread, in the thread you created it  in(possibly the main thread)... I will add, that the others are right, it's more difficult for us, with only a few bits of code... Regards Benny ---------------------------------------------------------------------- H₂SO₄ · 2024-11-29 06:06 Quote from: RedOctober on November 28, 2024, 09:57:25 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TISO_Parms = class    seg_txt_rbi: String;    req_lst, rsp_lst, fli_lst, flo_lst: TStringList;    sys_err: Boolean;    sys_err_txt: String;    db_data_rqrd: String;    dmISO: TDataModule;  end;  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetAllDashItems_ISO(ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse; AUserData: Pointer);begin  GetAllDashItemsIsolated(ARequest, AResponse,    AUserData^.ISO_Parms.seg_txt_rbi,  // <-- Getting error here:  Illegal Qualifier // ... of course, all the lines below have the same problem as the line aboveAUserData.req_lst, AUserData.rsp_lst, AUserData.fli_lst, AUserData.flo_lst,    AUserData.sys_err, AUserData.sys_err_txt, AUserData.db_data_rqrd,    AUserData.dmISO);end;  What is the proper syntax for dereferencing (accessing) the values in the parameters contained in my ISO_Parms object.  I have built isolated threads in the Brook 5 Framework before, with success, but I have never before included anything in the AUserData parameter. You'll need to cast  AUserData  to the correct type to dereference it; trying to dereference a  Pointer  is the same as trying to dereference a  void*  in C - it's simply not possible. This kind of untyped pointer commonly occurs in user-defined callback functions (such as yours here) to allow passing arbitrary data, at the expense of compiler-provided type checking. But there's another caveat -  TISO_Params  is a class type, and class instances themselves are just pointers in Free Pascal, so you can just use a normal cast: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TISO_Params(AUserData).seg_txt_rbi If  TISO_Params  was a  record  instead, you'd have to declare its pointer type as well, such that you're able to cast & dereference manually: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TISO_Params: record    seg_txt_rbi: String;    req_lst, rsp_lst, fli_lst, flo_lst: TStringList;    sys_err: Boolean;    sys_err_txt: String;    db_data_rqrd: String;    dmISO: TDataModule;  end;  PISO_Params = ^TISO_Params; procedure GetAllDashItems_ISO(ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse; AUserData: Pointer);begin  GetAllDashItemsIsolated(ARequest, AResponse,                        PISO_PARAMS(AUserData)^.seg_txt_rbi,  // [...]end; ---------------------------------------------------------------------- RedOctober · 2024-11-29 15:39 Hi KHrys, I have made the change in my source code to make ISO_Params a reccord instead of an Object.  That makes sense to me, because it relieves me of having to create, then free, an object.  However, I am having trouble handing the pointer to the isolated function.  See my code below:' I start by declaring the types you show in your example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TISO_Params = record    seg_txt_rbi: String;    req_lst, rsp_lst, fli_lst, flo_lst: TStringList;    sys_err: Boolean;    sys_err_txt: String;    db_data_rqrd: String;  end;  PISO_Params = ^TISO_Params;  I then declare a local copy of the TISO_Params, and populate it with the data contained in the variables in the main thread (this is the stuff that must end up in the isolated function, via the pointer).  The ARequest.Isolate() line is where the compiler shows an error "Illegal Type Conversion, TISO_Params to PISO_params" Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure MyMainThreadProc(...);  ...  var ISO_Params: TISO_Params;  ...begin     ISO_Params.seg_txt_rbi := seg_txt_rbi;      ISO_Params.req_lst := req_lst;      ISO_Params.rsp_lst := rsp_lst;      ISO_Params.fli_lst := fli_lst;      ISO_Params.flo_lst := flo_lst;      ISO_Params.sys_err := sys_err;      ISO_Params.sys_err_txt := sys_err_txt;      ISO_Params.db_data_rqrd := db_data_rqrd;     ARequest.Isolate(@GetAllDashItems_ISO, PISO_Params(ISO_Params)); // Trouble hereend;  The trouble line is: ARequest.Isolate(@GetAllDashItems_ISO, PISO_Params(ISO_Params)); So my question is: ARequest.Isolate(@GetAllDashItems_ISO, ); Thanks in advance. ---------------------------------------------------------------------- dseligo · 2024-11-29 15:46 Maybe: ARequest.Isolate(@GetAllDashItems_ISO, PISO_Params(@ISO_Params)); ---------------------------------------------------------------------- RedOctober · 2024-11-29 16:02 I have made the following modifications.  Both compile, but now the problem is dereferencing: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...  var ISO_Params: TISO_Params; Ptr: ^PISO_Params;...begin...      // Prepare an instance of ISO_Parms to hold all our stuff      ISO_Params.seg_txt_rbi := seg_txt_rbi;      ISO_Params.req_lst := req_lst;      ISO_Params.rsp_lst := rsp_lst;      ISO_Params.fli_lst := fli_lst;      ISO_Params.flo_lst := flo_lst;      ISO_Params.sys_err := sys_err;      ISO_Params.sys_err_txt := sys_err_txt;      ISO_Params.db_data_rqrd := db_data_rqrd;...// This compiles      Ptr := @ISO_Params;     ARequest.Isolate(@GetAllDashItems_ISO, Ptr); // This also compiles   //ARequest.Isolate(@GetAllDashItems_ISO, Ptr);   ARequest.Isolate(@GetAllDashItems_ISO,     PISO_Params(@ISO_Params)); // See the next code clip...end;  When dereferenced, the "thd_"  (local isolated thread) variables contain garbage, or cause a memory access error. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure GetAllDashItems_ISO(ARequest: TBrookHTTPRequest; AResponse: TBrookHTTPResponse; AUserData: Pointer);var  thd_seg_txt_rbi: String;  thd_req_lst,  thd_rsp_lst,  thd_fli_lst,  thd_flo_lst: TStringList;  thd_sys_err: Boolean;  thd_sys_err_txt,  thd_db_data_rqrd: String; begin  thd_req_lst := TStringList.Create;  thd_rsp_lst := TStringList.Create;  thd_fli_lst := TStringList.Create;  thd_flo_lst := TStringList.Create;   // Trouble starts here  thd_seg_txt_rbi := PISO_Params(AUserData)^.seg_txt_rbi;  thd_sys_err := PISO_Params(AUserData)^.sys_err;  thd_sys_err_txt := PISO_Params(AUserData)^.sys_err_txt;  thd_db_data_rqrd := PISO_Params(AUserData)^.db_data_rqrd;   thd_req_lst.AddStrings(PISO_Params(AUserData)^.req_lst);  thd_rsp_lst.AddStrings(PISO_Params(AUserData)^.rsp_lst);  thd_fli_lst.AddStrings(PISO_Params(AUserData)^.fli_lst);  thd_flo_lst.AddStrings(PISO_Params(AUserData)^.flo_lst);   GetAllDashItemsIsolated(    ARequest, AResponse,    thd_seg_txt_rbi,  thd_req_lst,    thd_rsp_lst,    thd_fli_lst,    thd_flo_lst,    thd_sys_err,    thd_sys_err_txt,    thd_db_data_rqrd);   thd_req_lst.Free;  thd_rsp_lst.Free;  thd_fli_lst.Free;  thd_flo_lst.Free; end;