Forum > General
Dereferencing an object handed into a thread by Pointer
RedOctober:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---ARequest.Isolate(@GetAllDashItems_ISO);
// Isolated procedure I am having trouble with
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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:
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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- 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<T> = 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<T>.Create(const AProc: T; AUserData: Pointer);begin inherited Create; FProc := AProc; FUserData := AUserData;end; procedure TBrookHTTPRequest.Isolate(AProc: TBrookHTTPRequestIsolatedProc; AUserData: Pointer);var VHolder: TBrookHTTPReqIsolatedProcHolder<TBrookHTTPRequestIsolatedProc>;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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- ARequest.Isolate(@GetAllDashItems_ISO, @ISO_Parms);
RedOctober:
I think I've got it now. I just had to change this last thing, using the modified code shown above:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- ARequest.Isolate(@GetAllDashItems_ISO, ISO_Parms);
Seems the last parameter must not be a pointer, only the first one.
Navigation
[0] Message Index
[#] Next page