Forum > General

Dereferencing an object handed into a thread by Pointer

<< < (3/3)

Khrys:

--- Quote from: RedOctober on November 28, 2024, 09:57:25 pm ---
--- 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; 

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

--- End quote ---

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  [+][-]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";}};} ---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  [+][-]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_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:
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  [+][-]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_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  [+][-]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 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, <what goes here?>);



Thanks in advance. 

dseligo:
Maybe:
ARequest.Isolate(@GetAllDashItems_ISO, PISO_Params(@ISO_Params));

RedOctober:
I have made the following modifications.  Both compile, but now the problem is dereferencing:


--- 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";}};} ---...  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  [+][-]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  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;  

Navigation

[0] Message Index

[*] Previous page

Go to full version