Jump to content
dkprojektai

DynArraySetLength exception

Recommended Posts

Hello, I have an exception "DynArraySetLength" in code:

 

var
Locking: TOmniMREW;
String_list: TStringList;

procedure TWorkerThread.Execute;
begin
  
  ...
  OtlParallel.Parallel.For(0, 20,1).NumTasks(6).Execute(
  procedure (value: integer)
  begin
    ...
    Locking.EnterReadLock;
    Try
      String_list.Add('text text'); ->>>> exception
    Finally
      Locking.ExitReadLock;
    End;
    ....
   end);
   ...

end;

What could be the problem?

 

I Use Delphi 10.2.3 and Omnithreadlibrary 3.07.7

Share this post


Link to post

I'm doubt. Here is full stack:

 

0040f509 ANPR_VID.exe System         35017 DynArraySetLength
0040f651 ANPR_VID.exe System         35119 @DynArraySetLength
004c0572 ANPR_VID.exe System.Classes  7281 TStringList.SetCapacity
004c0275 ANPR_VID.exe System.Classes  7172 TStringList.Grow
004c033d ANPR_VID.exe System.Classes  7197 TStringList.InsertItem
004bfed7 ANPR_VID.exe System.Classes  7008 TStringList.AddObject
004bfe7d ANPR_VID.exe System.Classes  6995 TStringList.Add
012816d4 ANPR_VID.exe Unit1          15764 TWorkerThread.Execute$ActRec.$2$Body
0111970e ANPR_VID.exe OtlParallel     3440 TOmniParallelSimpleLoop.Execute[2]$ActRec.$0$Body
01119228 ANPR_VID.exe OtlParallel     3380 TOmniParallelSimpleLoop.CreateForTask$ActRec.$0$Body
010f1943 ANPR_VID.exe OtlTaskControl  1991 TOmniTaskExecutor.Asy_Execute
010f06f2 ANPR_VID.exe OtlTaskControl  1584 TOmniTask.InternalExecute
0040a6ec ANPR_VID.exe System         17320 TObject.GetInterface
004124f3 ANPR_VID.exe System         38375 TInterfacedObject.QueryInterface
004110ce ANPR_VID.exe System         37361 @IntfCast
010e27f8 ANPR_VID.exe OtlThreadPool    889 TOTPWorkerThread.ExecuteWorkItem
010e2373 ANPR_VID.exe OtlThreadPool    846 TOTPWorkerThread.Execute
004d19c9 ANPR_VID.exe System.Classes 14945 ThreadProc
0040c388 ANPR_VID.exe System         24423 ThreadWrapper
755e0417 KERNEL32.DLL                      BaseThreadInitThunk

Share this post


Link to post
Just now, Primož Gabrijelčič said:

Compilable, working example, please.

Imposible. Very big project. I Think the problem is on TOmniMREW

Share this post


Link to post

I would think Add is a Write Operation, not a ReadLock ..

  • Like 2

Share this post


Link to post
45 minutes ago, dkprojektai said:

Imposible. Very big project. I Think the problem is on TOmniMREW

Cut it down to a minimal repro. Although it is likely that the answer has been revealed in other comments about the misuse of the lock.

Edited by David Heffernan

Share this post


Link to post
38 minutes ago, dkprojektai said:

Yes it is, But I use it just for locking. Or I'm wrong?

Yes, you are wrong. If you have two threads reading then they are not synchronisation with respect to each other. 

  • Like 1

Share this post


Link to post
4 hours ago, Rollo62 said:

I would think Add is a Write Operation, not a ReadLock ..

Yes, exactly.  It needs a write lock, not a read lock.

4 hours ago, dkprojektai said:

Yes it is, But I use it just for locking. Or I'm wrong? 

Yes, you are wrong.  A read lock allows multiple threads to read shared data at the same time.  A write lock allows only 1 thread at a time to modify shared data.  Since you are modifying data (adding a new string to the TStringList, thus modifying its internal memory array and its Count), you need a write lock.  You are getting an AV because your read lock is allowing multiple threads to modify the TStringList at the same time, trampling on each other's toes, so to speak.

Edited by Remy Lebeau
  • Like 5

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×