Error when using strings in TOmniValue array in a BackgroundWorker

我只是一个虾纸丫 提交于 2019-12-24 03:07:44

问题


When I have a string in a TOmniValue array, then accessing the value by name or by explicit index raises an access violation. See the following code for an example. Am I doing something wrong, or is there an error in Delphi or TOmniValue? I found a workaround for accessing by index, but what about by name?

UPDATE: I've moved Test into a console app for easier reproduction, but now the error only occurs in every second run or so instead of every interation. Or maybe it is not even the same error now?

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  OtlParallel,
  OtlCommon;

procedure Test;
const
  Count = 1000;
var
  i: integer;
  BackgroundWorker: IOmniBackgroundWorker;
begin
    BackgroundWorker := Parallel.BackgroundWorker.NumTasks(2)
      .Execute(
        procedure (const workItem: IOmniWorkItem)
        const Inx_ = 1;
        var Inx: integer;
        begin
          // Works:
          workItem.Result := 'Integer: '+workItem.Data[workItem.Data.AsArray.Count-1].AsString;
          // Works (but why?!):
          workItem.Result := 'Integer: '+workItem.Data[workItem.Data.AsArray.Count*0+Inx_].AsString;
          // Raises AV immediately:
          workItem.Result := 'Integer: '+workItem.Data[Inx_].AsString;
          // Raises AV immediately:
          Inx:=workItem.Data.AsArray.Count-1;
          workItem.Result := 'Integer: '+workItem.Data[Inx].AsString;
          // Raises AV later when workItem.Result is accessed:
          workItem.Result := 'Integer: '+workItem.Data['BB'].AsString;
        end
      )
      .OnRequestDone(
        procedure (const Sender: IOmniBackgroundWorker;
          const workItem: IOmniWorkItem)
        begin
          workItem.Result.AsString;
        end)
    ;
    for i := 1 to Count do begin
      BackgroundWorker.Schedule(BackgroundWorker.CreateWorkItem(
        TOmniValue.CreateNamed(
          [
            'AA',IntToStr(Random(100)),
            'BB',IntToStr(Random(100))
          ]
        )
      ));
    end;
    BackgroundWorker.WaitFor(INFINITE);
end;

begin
  Test;
end.

I'm using the newest trunk of Otl (r1333). I believe I had the same error in 3.03a.

来源:https://stackoverflow.com/questions/23994202/error-when-using-strings-in-tomnivalue-array-in-a-backgroundworker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!