program locks up using setlength(array,n) on Packed Array of byte lazarus / 62-General ---------------------------------------------------------------------- dieselnutjob · 2023-06-06 17:40 Hi I have a program that's locking up (this bit of code is buried deep in a thread so I don't exactly know how) when I invoke setlength on a variable length array. The definition is:- ABlock: Packet Array of byte; Previously in the code the length is set to zero. When I do setlength(ABlock,n) it locks up, and if I remove that one line, it doesn't. It seems to be specific to fpc 3.0.4 an MacOS Anyone got any ideas why? thanks ---------------------------------------------------------------------- PascalDragon · 2023-06-06 19:47 Without more code it's hard to tell. Can the variable be accessed from different threads at the same time? ---------------------------------------------------------------------- dieselnutjob · 2023-06-06 22:06 The array is only touched in the one thread. ---------------------------------------------------------------------- 440bx · 2023-06-06 22:15 Is ABlock in a "var" section or in a "const" section ? if it happens to be in a "const" section that might be the cause. ---------------------------------------------------------------------- dieselnutjob · 2023-06-06 22:19 it's in a var section immediately after "implementation" ---------------------------------------------------------------------- dieselnutjob · 2023-06-06 22:26 aah It's not a var inside any function, and all the functions belong to a class(object) thing. So I suppose that vars are immediately after "implementation" belong to the unit but not the object? Maybe that doesn't matter if they are fixed length but maybe it does matter if they are variable length? Because a function in an object is changing the length of a thing that's not part of the object? Could that be it or am I talking rubbish? ---------------------------------------------------------------------- 440bx · 2023-06-07 06:29 Quote from: dieselnutjob on June 07, 2023, 12:26:40 amCould that be it or am I talking rubbish? Without seeing the code, it's really difficult to say.  The reason I mentioned "const" vs "var" is because if the dynamically sized array is in a read-only const section then a SetLength would cause an exception (the pointer that identifies/references the array cannot be assigned a value because it cannot be written to.) ---------------------------------------------------------------------- TRon · 2023-06-07 07:12 Quote from: 440bx on June 07, 2023, 08:29:33 amQuote from: dieselnutjob on June 07, 2023, 12:26:40 amCould that be it or am I talking rubbish? Without seeing the code, it's really difficult to say.  +1 Everything in the implementation section (not part of an/the thread object/class) is part of the main thread. If you create another thread that access such declarations then you have multiple threads accessing a single variable. Using setlength implies that there needs to be memory allocated (amongst others) and since you are doing that from the 'other' thread (not mainthread) that could lead to issues. There even is a terminology for that named threadsafe (programming). ---------------------------------------------------------------------- PascalDragon · 2023-06-08 10:53 Quote from: dieselnutjob on June 07, 2023, 12:26:40 amIt's not a var inside any function, and all the functions belong to a class(object) thing. That's an important difference. Quote from: dieselnutjob on June 07, 2023, 12:26:40 amSo I suppose that vars are immediately after "implementation" belong to the unit but not the object? Correct. Quote from: dieselnutjob on June 07, 2023, 12:26:40 amMaybe that doesn't matter if they are fixed length but maybe it does matter if they are variable length? If you modify the contents even if the length stays the same then it always does matter if it might be accessed from multiple threads. ---------------------------------------------------------------------- cdbc · 2023-06-08 14:05 Hi You've piqued my interest... So here goes, attached is a small class and interface, to be used in multithreaded access to a "packed array of byte". If it can be of use to you, then I've got a quick'n'dirty use- 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 uses ..., u_clspab, u_intfpab;var  s: shortstring;  cls: TThreadsafePackedArrayOfByte;  pab: IThreadsafePackedArrayOfByte; {$region pab}function NewArray(aSize: ptruint): IThreadsafePackedArrayOfByte;begin  cls:= TThreadsafePackedArrayOfByte.Create(aSize);  if not cls.GetInterface(SGUIDIThreadsafePackedArrayOfByte,Result) then Result:= nil;end;procedure DisposeArray; begin FreeAndNil(cls); pab:= nil; end;procedure WriteArray(aBuf: IThreadsafePackedArrayOfByte);var a: TPAoB; s: string;begin  a:= aBuf.LockArray;  try    SetLength(s,Length(a));    move(a[0],s[1],Length(a));    writeln('*) Array of byte: ',s);    s:= ''  finally aBuf.UnLockArray; end;end;{$endregion pab}...  S:= 'Lyserøde GyngehesteRøvhuller til salg';  pab:= NewArray(Length(S));  try    move(S[1],pab.LockArray[0],Length(S));    pab.UnLockArray; { !!! very important !!! as we use it without try...finally}    WriteArray(pab);    pab.SetSize(0);  finally DisposeArray; end;  writeln('Done!'); /// NB.: if you change the interface to com and "TObject" to //////  "TInterfacedObject", you can do it like this: /////  pab:= TThreadsafePackedArrayOfByte.Create(Length(S));//  ... use pab//  pab:= nil; // refcounting frees it Let me know what you think... Regards Benny ---------------------------------------------------------------------- dieselnutjob · 2023-06-08 22:31 I got my code to work today (before reading cbdb's reply). I got it to work by changing the variable length array to a fixed length one, and now it all works. I have other functions in other parts of the code that use variable length arrays, and they work, but there is one crucial difference which is that these array are defined inside the function that touches the array. So it seems to me that with 3.0.4 on MacOS that:- You can have a fixed length array in the var section after implementation, before all the functions, so accessible to all functions. or You can have a variable length array in the var section of just the function that uses it. If you want a variable length array that's accessed by multiple functions (even if they are in the same thread) then it seems to be a problem (but only in MacOS). ---------------------------------------------------------------------- cdbc · 2023-06-09 07:34 Hi Good on you  I seem to remember, I once had an issue with dynamic arrays, they would crash if I didn't initialize them, with just one byte on creation, so that they were not nil, when I needed them... You might need input on this from @PascalDragon or colleagues, as I think it's above my paygrade  That's also why I used this: "fPAB:= [];" construct, in my class' constructor, as that seems to be dabbling as "constructor" for dynamic arrays. Anyway, great that you got it to work  Regards Benny ---------------------------------------------------------------------- dieselnutjob · 2023-06-09 12:51 the wiki page https://wiki.freepascal.org/Dynamic_array mentions initialising, but I got the impression that it was optional. so for an empty array should I be doing myarray.create(); ? ---------------------------------------------------------------------- cdbc · 2023-06-09 14:44 Hi I've just read the wiki too, and yes there are "more ways to skin that cat". - The object oriented way: with .Create(0,1,2,3); // from wiki - The subtle way: iArray:= [7, 8, 9]; // from wiki - The declaration way: iArray: array of byte = (4,5,6); // from wiki I find the [] subtle way leans itself more towards array-syntaks, as I know it, easier on the eye, if you will  I guess it comes down to preference and size of dataset... Mind you, I'll just check the fpc-reference now... edit: According to the reference, all 3 are valid from version 3.2 and upwards.  Regards Benny ---------------------------------------------------------------------- PascalDragon · 2023-06-09 20:15 Quote from: dieselnutjob on June 09, 2023, 12:31:35 amIf you want a variable length array that's accessed by multiple functions (even if they are in the same thread) then it seems to be a problem (but only in MacOS). Doubtful. But without explicit source code it's hard to tell what you're doing wrong.