Solved

access violation on tbits.bits when tbits.size = high(integer)

Posted on 2011-09-16
5
614 Views
Last Modified: 2012-05-12
I'm using a list of boolean flags to indicate primenumbers by index
This works fine for lists up to 2 billion entries but as soon as I set the size of the list (tbits.size) to high(integer
) I get an access violation when I try to set the flags, like so:
for d := 0 to list.size-1 do  list[d] := true;
Where d is a local variable of type integer
As an added bonus when I trace throught the code up to the point where the exception occurs the evaluator claims that d is an undeclared identifier this bit of strangeness can be avoided by making d global instead of local this however does not fix the access violation-issue. I use Delphi XE on Wondows 7 x64 SP1. Access violation would sugest a nil-pointer but I checked and the object is created fine. Any ideas anybody.
TIA
Psydo.
0
Comment
Question by:Psychododo
  • 4
5 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 36554436
I'll have to check in Delphi code for TBits, of course I suspect some calculation that goes over some inherent limit.

does it makes AV for a big value for v or even the first ones, just because Size is too big ?
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36554453
ok, there is ASM code here, it will take some time to get documentation (and possible limitations) of operator BTS (Bit Set)

Other than that I can only see the possible overflow when calculating the size of the memory.
If it can work with Size := High(Integer) Div 4 but not just above, then the problem is here.

Can't do tests right now, can you ?
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 36554490
When you have values > High(Integer)-31, the calculated size (which is not done on 64bits int) will overflow (and then be negative). Which will fail on GetMem, thus returning nil for pointer.

So accept this limitation or copy TBits code in Classes unit and fix this limitation
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36554501
replacing Integer with Cardinal will double the capacity,
and stating Int64 in the formula will force 64 bit precision for the values around the limit. That would be the 31 last values, not sure that it's worth it. But Cardinal trick is totally free and easy
procedure TBits.SetSize(Value: Cardinal);
var
  NewMem: Pointer;
  NewMemSize: Cardinal;
  OldMemSize: Cardinal;

  function Min(X, Y: Integer): Integer;
  begin
    Result := X;
    if X > Y then Result := Y;
  end;

begin
  if Value <> Size then
  begin
    if Value < 0 then Error;
    NewMemSize := ((Int64(Value) + BitsPerInt - 1) div BitsPerInt) * SizeOf(Cardinal);
    OldMemSize := ((Int64(Size) + BitsPerInt - 1) div BitsPerInt) * SizeOf(Cardinal);
    if NewMemSize <> OldMemSize then
    begin
      NewMem := nil;
      if NewMemSize <> 0 then
      begin
        GetMem(NewMem, NewMemSize);
        FillChar(NewMem^, NewMemSize, 0);
      end;
      if OldMemSize <> 0 then
      begin
        if NewMem <> nil then
          Move(FBits^, NewMem^, Min(OldMemSize, NewMemSize));
        FreeMem(FBits, OldMemSize);
      end;
      FBits := NewMem;
    end;
    FSize := Value;
  end;
end;

Open in new window

0
 

Author Closing Comment

by:Psychododo
ID: 36554566
Excellent Epasquier, I think I'll just accept the limitation for now as my version of Delphi comes without sources there will be no fixing of code. However I am relieved to know the voodoo occurs in the Tbits-object and is not of my own doing.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question