EDIT: This problem has been solved... see my later post.
I'm getting strange bugs using advanced records with methods and a dynamic array inside.
In the following scenario...
type R=record {various bits'n'bobs} end;
var u,v:R;
procedure P(const p1:R; var p2:R); begin p2:=0 end;
begin
u:= 1;v:= u;
P(v);
// after the procedure call, the u variable also gets the value zero!
...then after the procedure call, both the u & v variables get overwritten. This happens to the p1 and p2 parameters inside the procedure, and in u & v variables outside the procedure. And it happens,
even though the u variable is passed to the procedure as a const parameter.
NB this problem is very intermittent; the above code is "example" code, and not guaranteed to run or produce the problem.
EDIT: I forgot to say that if the procedure is defined with an out parameter instead of a var parameter like this, then the problem disappears:
procedure P(const p1:R; out p2:R);
I have made some progress: it apears that when the assignment v:=u is done, the compiler sometimes copies a reference and not the record data. Then, when the v variable is accessed inside the procedure, it also overwrites the u variable.
I have worked around the problem, by creating a procedure that explicitly copies one record to another, e.g.
procedure copy_R(var P1,P2);begin P2.field:= P1.field; etc;end;
and use that instead of assignments like v:=u;
The problem is that I am creating a library, and if users of my library also hit the same problem, then they are going to get very strange bugs that will be impossible to fix.
I can't post my actual code because there is too much. I will have another attempt to try to produce some minimal code that shows the problem, and if successful I will post a follow-up.
I've been battling with this problem for many months now, and would welcome any suggestions. I have searched the internet but didn't get any good results. I am getting desperate.
Thanks.