Recent

Author Topic: Frustrating Error When using getmem to allocate memory and dare to ask  (Read 2719 times)

TYDQ

  • Full Member
  • ***
  • Posts: 176
These days I am developing an assembler for assembling assembly code generated by FPC(default unias is a absolute file path,you could edit it to point to the real system.s).
However,When my code execute in Line 3417 to Line 3421 in unianalysis.pas to generate the ELF object file,My Program crashed in the Line 3420 just about allocate the memory(using getmem):
Code: Pascal  [Select][+][-]
  1.  writeln('1',' ',OutputFile.SectionSize[OutputFile.SectionCount-1]);
  2.      writeln(TempFile.Section[i-1].SectionType);
  3.      OutputFile.SectionContent[OutputFile.SectionCount-1]:=
  4.      getmem(OutputFile.SectionSize[OutputFile.SectionCount-1]); #Where the program crashes
  5.      writeln('2');
Attachments about my project is the extra,if you demand solve my problem,download my attachment.

bytebites

  • Hero Member
  • *****
  • Posts: 778
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #1 on: February 13, 2026, 09:36:37 am »
You should set range check on.
Use {$push}{$r-} {$pop} when the range violation is allowed.

Zvoni

  • Hero Member
  • *****
  • Posts: 3315
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #2 on: February 13, 2026, 10:02:56 am »
I seem to remember, that you should never use the Property of a Class directly with such calls.
Use an intermediary variable with Type PtrUInt
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TYDQ

  • Full Member
  • ***
  • Posts: 176
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #3 on: February 13, 2026, 10:25:12 am »
I seem to remember, that you should never use the Property of a Class directly with such calls.
Use an intermediary variable with Type PtrUInt
It is not a class but a record,et cetera.
I enable the range checking(using -Cr) and edit then but It still give a same error as these code:
Code: Pascal  [Select][+][-]
  1. writeln('1',' ',OutputFile.SectionSize[OutputFile.SectionCount-1]);
  2.      Content:=getmem(OutputFile.SectionSize[OutputFile.SectionCount-1]) //The error raised internally in getmem and these are not classes,all members of OutputFile are not classes but members.
  3.      OutputFile.SectionContent[OutputFile.SectionCount-1]:=Content;
  4.      writeln('2');

Zvoni

  • Hero Member
  • *****
  • Posts: 3315
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #4 on: February 13, 2026, 12:13:41 pm »
I seem to remember, that you should never use the Property of a Class directly with such calls.
Use an intermediary variable with Type PtrUInt
It is not a class but a record,et cetera.
I enable the range checking(using -Cr) and edit then but It still give a same error as these code:
Code: Pascal  [Select][+][-]
  1. writeln('1',' ',OutputFile.SectionSize[OutputFile.SectionCount-1]);
  2.      Content:=getmem(OutputFile.SectionSize[OutputFile.SectionCount-1]) //The error raised internally in getmem and these are not classes,all members of OutputFile are not classes but members.
  3.      OutputFile.SectionContent[OutputFile.SectionCount-1]:=Content;
  4.      writeln('2');
Aha

So "Outputfile" is a Record, and "SectionSize" is an Array of PtrUint?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

jamie

  • Hero Member
  • *****
  • Posts: 7610
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #5 on: February 13, 2026, 12:34:41 pm »
Is it possible you are entering with a sectioncount of 0 and subtracting 1 places it into no mans land?
?
The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 3315
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #6 on: February 13, 2026, 01:02:56 pm »
Is it possible you are entering with a sectioncount of 0 and subtracting 1 places it into no mans land?
?
Shouldn't he then get a KABOOM, since he would pass -1 as the Array-index for SectionSize?

And he's doing a "Debug"-Output with his Writeln.

Specifically his line 2: What IS the Value of SectionSize for this "last" Array-Member? Because if it's zero.....

EDIT: And the "OutputFile"-Member "SectionContent" is an Array of Pointers?
« Last Edit: February 13, 2026, 01:09:52 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TYDQ

  • Full Member
  • ***
  • Posts: 176
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #7 on: February 13, 2026, 01:35:52 pm »
Is it possible you are entering with a sectioncount of 0 and subtracting 1 places it into no mans land?
?
No,I tested the sectioncount that when the crash happens,it is 86 not the 0.

TYDQ

  • Full Member
  • ***
  • Posts: 176
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #8 on: February 13, 2026, 01:38:46 pm »
Is it possible you are entering with a sectioncount of 0 and subtracting 1 places it into no mans land?
?
Shouldn't he then get a KABOOM, since he would pass -1 as the Array-index for SectionSize?

And he's doing a "Debug"-Output with his Writeln.

Specifically his line 2: What IS the Value of SectionSize for this "last" Array-Member? Because if it's zero.....

EDIT: And the "OutputFile"-Member "SectionContent" is an Array of Pointers?
SectionContent is really an Array of Pointers.Its definition is in unianalyze.pas:
Code: Pascal  [Select][+][-]
  1. unias_final_file=packed record
  2.                       {For Section Name Position in Section Header String Table}
  3.                       SectionNameIndex:array of Dword;
  4.                       {For Section and Contents}
  5.                       SectionName:array of string;
  6.                       SectionType:array of Dword;
  7.                       SectionFlag:array of SizeUint;
  8.                       SectionOffset:array of SizeUint;
  9.                       SectionSize:array of Dword;
  10.                       SectionAlign:array of Dword;
  11.                       SectionEntrySize:array of Dword;
  12.                       SectionContent:array of Pointer;
  13.                       SectionCount:word;
  14.                       {For Symbol Table and Corresponding String Table and Section Header String Table}
  15.                       SymbolStringTableContent:Pointer;
  16.                       SymbolStringTableIndex:word;
  17.                       SymbolStringTableSize:SizeUint;
  18.                       SymbolTableContent:Pointer;
  19.                       SymbolTableIndex:word;
  20.                       SymbolTableSize:SizeUint;
  21.                       StringTableContent:Pointer;
  22.                       StringTableIndex:word;
  23.                       StringTableSize:SizeUint;
  24.                       {For Section Search}
  25.                       SectionHash:unias_hash_table;
  26.                       end;

TYDQ

  • Full Member
  • ***
  • Posts: 176
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #9 on: February 13, 2026, 01:43:32 pm »
I seem to remember, that you should never use the Property of a Class directly with such calls.
Use an intermediary variable with Type PtrUInt
It is not a class but a record,et cetera.
I enable the range checking(using -Cr) and edit then but It still give a same error as these code:
Code: Pascal  [Select][+][-]
  1. writeln('1',' ',OutputFile.SectionSize[OutputFile.SectionCount-1]);
  2.      Content:=getmem(OutputFile.SectionSize[OutputFile.SectionCount-1]) //The error raised internally in getmem and these are not classes,all members of OutputFile are not classes but members.
  3.      OutputFile.SectionContent[OutputFile.SectionCount-1]:=Content;
  4.      writeln('2');
Aha

So "Outputfile" is a Record, and "SectionSize" is an Array of PtrUint?
It is a SizeUint(PtrUint is the same).
However,the crash is on SysGetHeapStatus of pre-built system.pas as I debug with Lazarus.

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #10 on: February 13, 2026, 06:52:20 pm »
Simplified example:
Code: Pascal  [Select][+][-]
  1. var
  2.   Outputfile: unias_final_file;
  3.   tmp: Pointer;
  4. begin
  5.       //initialize some fields
  6.       OutPutFile.SectionCount := 1;
  7.       SetLength(OutPutFile.SectionContent,1);
  8.       SetLength(OutputFile.SectionSize,1);
  9.       OutputFile.SectionSize[0] := 256;
  10.  
  11.       writeln('1');
  12.       writeln(OutputFile.SectionSize[OutputFile.SectionCount-1]);
  13.       tmp := getmem(OutputFile.SectionSize[OutputFile.SectionCount-1]);
  14.       OutputFile.SectionContent[OutputFile.SectionCount-1] := tmp;
  15.       writeln('2');
  16.  
  17.       FreeMem(OutputFile.SectionContent[OutputFile.SectionCount-1]);
  18. end.

Outputs:
Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
1
256
2
Heap dump by heaptrc unit of C:\Users\Bart\LazarusProjecten\ConsoleProjecten\test.exe
3 memory blocks allocated : 280/288
3 memory blocks freed     : 280/288
0 unfreed memory blocks : 0
True heap size : 131072 (112 used in System startup)
True free heap : 130960

So, you need to check the value of each variable you use in your code to see if anything is not what it is supposed to be.


B.t.w. your project (as attached in your first post) does not compile: unihash.pas(17,10) Error: Forward declaration not solved "unihash_generate_value(AnsiString):QWord;"
This is because you define:
Code: [Select]
function unihash_generate_value(str:string):qword;
....
function unihash_generate_value(str:string):SizeUint;
begin
 if(str='') then exit(0);
 Result:=unihash_city_hash_64(Pointer(str),length(str));
end;
On 32-bit SizeUInt<>QWord

(Also there are some illegal typecastst to SizeInt (when compiled for 32-bit).)

If I fix this, compilation succeeds.
When built for 64-bit Windows the program crashes in a different place:
Code: [Select]
C:\Users\Bart\LazarusProjecten\bugs\forum\unias\unias>unias
An unhandled exception occurred at $0000000100098872:
EIntOverflow: Arithmetic overflow
  $0000000100098872  unihash_hash_length_0_to_16,  line 75 of unihash.pas
  $0000000100099021  unihash_city_hash_64,  line 145 of unihash.pas
  $00000001000994F0  unihash_generate_value,  line 175 of unihash.pas
  $000000010003DF8A  unias_authority_add,  line 930 of uniasinfo.pas
  $0000000100041AF9  unias_initialize_architecture,  line 1365 of uniasinfo.pas
  $000000010000183D  $main,  line 9 of unias.lpr
  $0000000100001A16
  $00000001000118C0
  $00000001000017A0
  $00007FFB9C22E8D7
  $00007FFB9CF8C40C

So, at this point I give up.
If this program is NOT supposed to do anything under Windows, then insert a compiler message like {$fatal this program is only supposed to compile under xxx}.

Bart

PS. Some whitespace (empty lines) between procedures would increase readability.
And a single procedure of 461 lines is not helping there as well.
« Last Edit: February 13, 2026, 07:14:20 pm by Bart »

TYDQ

  • Full Member
  • ***
  • Posts: 176
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #11 on: February 14, 2026, 01:38:26 am »
Simplified example:
Code: Pascal  [Select][+][-]
  1. var
  2.   Outputfile: unias_final_file;
  3.   tmp: Pointer;
  4. begin
  5.       //initialize some fields
  6.       OutPutFile.SectionCount := 1;
  7.       SetLength(OutPutFile.SectionContent,1);
  8.       SetLength(OutputFile.SectionSize,1);
  9.       OutputFile.SectionSize[0] := 256;
  10.  
  11.       writeln('1');
  12.       writeln(OutputFile.SectionSize[OutputFile.SectionCount-1]);
  13.       tmp := getmem(OutputFile.SectionSize[OutputFile.SectionCount-1]);
  14.       OutputFile.SectionContent[OutputFile.SectionCount-1] := tmp;
  15.       writeln('2');
  16.  
  17.       FreeMem(OutputFile.SectionContent[OutputFile.SectionCount-1]);
  18. end.

Outputs:
Code: [Select]
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
1
256
2
Heap dump by heaptrc unit of C:\Users\Bart\LazarusProjecten\ConsoleProjecten\test.exe
3 memory blocks allocated : 280/288
3 memory blocks freed     : 280/288
0 unfreed memory blocks : 0
True heap size : 131072 (112 used in System startup)
True free heap : 130960

So, you need to check the value of each variable you use in your code to see if anything is not what it is supposed to be.


B.t.w. your project (as attached in your first post) does not compile: unihash.pas(17,10) Error: Forward declaration not solved "unihash_generate_value(AnsiString):QWord;"
This is because you define:
Code: [Select]
function unihash_generate_value(str:string):qword;
....
function unihash_generate_value(str:string):SizeUint;
begin
 if(str='') then exit(0);
 Result:=unihash_city_hash_64(Pointer(str),length(str));
end;
On 32-bit SizeUInt<>QWord

(Also there are some illegal typecastst to SizeInt (when compiled for 32-bit).)

If I fix this, compilation succeeds.
When built for 64-bit Windows the program crashes in a different place:
Code: [Select]
C:\Users\Bart\LazarusProjecten\bugs\forum\unias\unias>unias
An unhandled exception occurred at $0000000100098872:
EIntOverflow: Arithmetic overflow
  $0000000100098872  unihash_hash_length_0_to_16,  line 75 of unihash.pas
  $0000000100099021  unihash_city_hash_64,  line 145 of unihash.pas
  $00000001000994F0  unihash_generate_value,  line 175 of unihash.pas
  $000000010003DF8A  unias_authority_add,  line 930 of uniasinfo.pas
  $0000000100041AF9  unias_initialize_architecture,  line 1365 of uniasinfo.pas
  $000000010000183D  $main,  line 9 of unias.lpr
  $0000000100001A16
  $00000001000118C0
  $00000001000017A0
  $00007FFB9C22E8D7
  $00007FFB9CF8C40C

So, at this point I give up.
If this program is NOT supposed to do anything under Windows, then insert a compiler message like {$fatal this program is only supposed to compile under xxx}.

Bart

PS. Some whitespace (empty lines) between procedures would increase readability.
And a single procedure of 461 lines is not helping there as well.
Google City Hash must be used in disabling overflow switch to work.
I don't do anything in 32-bit due to I work on 64-bit machines.
Don't Simplified the process due to the error is on system.pas as GDB backtrace said although I don't know why.

TYDQ

  • Full Member
  • ***
  • Posts: 176
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #12 on: February 14, 2026, 10:24:30 am »
Is it possible you are entering with a sectioncount of 0 and subtracting 1 places it into no mans land?
?
Shouldn't he then get a KABOOM, since he would pass -1 as the Array-index for SectionSize?

And he's doing a "Debug"-Output with his Writeln.

Specifically his line 2: What IS the Value of SectionSize for this "last" Array-Member? Because if it's zero.....

EDIT: And the "OutputFile"-Member "SectionContent" is an Array of Pointers?
How do you think to solve this problem?

jamie

  • Hero Member
  • *****
  • Posts: 7610
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #13 on: February 14, 2026, 04:02:32 pm »
You have a problem at the start of the Assembler read where it reads from the file initially loaded by a Tstringlist and used as a content.

That section of code wipes out your memory pool with something you are doing like repeating looping and or fragmentation.

Jamie
The only true wisdom is knowing you know nothing

TYDQ

  • Full Member
  • ***
  • Posts: 176
Re: Frustrating Error When using getmem to allocate memory and dare to ask
« Reply #14 on: February 14, 2026, 04:15:22 pm »
You have a problem at the start of the Assembler read where it reads from the file initially loaded by a Tstringlist and used as a content.

That section of code wipes out your memory pool with something you are doing like repeating looping and or fragmentation.

Jamie
So do you have a skilled idea about read the content of Assembly using another method or another way? :(

 

TinyPortal © 2005-2018