Lazarus
Free Pascal => General => Topic started by: Tommi on July 11, 2018, 02:27:44 pm
-
This is my code attempts to convert a byte array to single:
function bytes2float(var dd:TMioR;tipo:byte;Precision:byte;aa:Tbytes;Posiz:integer):single; overload; //Prcision in byte non in bit
var
oo:array [0..3] of byte;
begin
try
if tipo = 0 then
begin
if aa[posiz]<128 then
begin
if Precision = 1 then result:=aa[posiz];
if Precision = 2 then result:=aa[posiz]*256+aa[posiz+1];
end
else
begin
if Precision = 1 then result:=-(255-aa[posiz]+1);
if Precision = 2 then result:=-(65535-(aa[posiz]*256+aa[posiz+1])+1);
end;
end;
if tipo = 1 then
begin
oo[3]:=aa[Posiz];
oo[2]:=aa[Posiz+1];
oo[1]:=aa[Posiz+2];
oo[0]:=aa[Posiz+3];
move(oo,result,4);
if IsNAN(result) then result:=0;
end;
Except
on E : Exception do
begin
result:=0;
setlength(dd.Errori,length(dd.Errori)+1);
dd.Errori[Length(dd.Errori)-1].Testo:='Trovato errore generico.';
dd.Errori[Length(dd.Errori)-1].Codice:=8;
end;
end;
end;
When tipo=1, sometime in 32bit this code crashes. In 64 works.
Some idea ?
Thank you
-
begin
if Precision = 1 then result:=-(255-aa[posiz]+1);
if Precision = 2 then result:=-(65535-(aa[posiz]*256+aa[posiz+1])+1);
end;
end;
Are you sure that if Precision = 1 then result:=-(255-aa[posiz]+1)
is correct and not
if Precision = 1 then result:=-(255-aa[posiz+1])
At all other places the $+1$ is in the index expression.
-
It isn't there the prblem because it happens when tipo = 1 so when I am here:
if tipo = 1 then
begin
oo[3]:=aa[Posiz];
oo[2]:=aa[Posiz+1];
oo[1]:=aa[Posiz+2];
oo[0]:=aa[Posiz+3];
move(oo,result,4);
if IsNAN(result) then result:=0;
end;
-
Sorry, you are right. I was confused by the indentation.
-
specifically the problem borns when :
oo[0]=0
oo[1]=0
oo[2]=0
oo[1]=63
if I put result:=0.5 in the line above if IsNAN(result) then result:=0; the crash disappears.
-
The error is indipendent from the content of the function because if I just put result:=0; in the first row of the function, the application crashes there.
-
Issue solved creating a new project and copying the units. 32bit version of lazarus cannot open 64bit created projects because it gives this random errors.
-
Issue solved creating a new project and copying the units. 32bit version of lazarus cannot open 64bit created projects because it gives this random errors.
That would be a bug.
-
In few words: my code was working good compiled with 64bit version of lazarus. I opened the same project with 32bit and I had this problem. I reopened the same files (already modified with 32 bit version) with 64bit, and I had a lot of issues. GDB crashing and exe project remaining in memory with no hope to close it from taskmanager, reboot was necessary.
I created a new project copying the units and everything started to work fine.
So I tried also to copy lpi, lpr, lps, res files coming from an old 64 bit version (not touched from the 32) in the ruined project and it came back to work fine (compiling with 64bit).
Yes, I am pretty sure that it is a bug, but it is very difficult to create a minimal project to reproduce it.