#1 2017-09-11 16:50:11

jonsafi
Member
Registered: 2011-07-26
Posts: 58

TDynArrayHashed:Trouble with loading file saved file...

Hello,

Was using successfully TDynArray and saving and loading the data
from a file. Since the file generated was a bit large, tried
switching to TDynarrayHashed instead..

However, keep getting an access violation at the
line:
DynArray(TypeInfo(TStudents),MyStudents).LoadFromStream(MStream_);
when trying to load data from the file into hashed array.

Any comments/hints would be much appreciated.

P.S. Am using an older version of the framework, released in Feb '17.


// simple student DB
type
 TStudent = packed record
    sEmail: string;
    sGPA,sTotalCredits:Currency;
    sStudentId: string;
    sGender,sFullName: string;
end;
 TStudents = array of TStudent;


Var
  MyStudents: TStudents;
  aStudent  :   TStudent;
  aDynArray:  TDynArrayHashed;
  MStream_ : TMemoryStream;

 StudentsCount: Integer;

BEGIN
    MStream_ := TMemoryStream.Create;
  MStream_.LoadFromFile(GG_STUD_DIR +  GG_S_
                    +  'Stud_Info' + GG_TXT);

aDynArray.Init(TypeInfo(TStudents),MyStudents, nil, nil, nil, @StudentsCount);
     aDynArray.Capacity := 1200000; // should be enough
  DynArray(TypeInfo(TStudents),MyStudents).LoadFromStream(MStream_); // access violation

   MStream_.Free; 

END;

Offline

#2 2017-09-11 17:55:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,272
Website

Re: TDynArrayHashed:Trouble with loading file saved file...

Try first with a new revision of the framework, to check if there is not already a fixed issue.

Also don't use TMemoryStream + LoadFromFile, but call directly StringFromFile() function.

Offline

#3 2017-09-11 20:41:44

Junior/RO
Member
Registered: 2011-05-13
Posts: 207

Re: TDynArrayHashed:Trouble with loading file saved file...

Change

  DynArray(TypeInfo(TStudents),MyStudents).LoadFromStream(MStream_); // access violation

to

aDynArray(TypeInfo(TStudents),MyStudents).LoadFromStream(MStream_);

Offline

#4 2017-09-12 08:37:16

jonsafi
Member
Registered: 2011-07-26
Posts: 58

Re: TDynArrayHashed:Trouble with loading file saved file...

Hello,

Thanks a lot for both your help.

Using:

aDynArray(TypeInfo(TStudents),MyStudents).LoadFromStream(MStream_)
would not compile,  perhaps the error is elsewhere..

Anyhow, got rid of the memory streams and using the following
code no longer produces an access error,
but now fails to find the given student (which should be in the file).

Still need to try this with a new framework...

Var
     Raw_:        RawByteString;
indx_,StudentsCount:Integer;

BEGIN
Raw_  := BinToBase64(StringFromFile(GG_STUD_DIR +  GG_S_
                    +  'Stud_Info' + GG_TXT));
StudentsCount := 0;
aDynArray.Init(TypeInfo(TStudents),MyStudents, nil, nil, nil, @StudentsCount);

aDynArray.Capacity := 120000; 
DynArray(TypeInfo(TStudents),MyStudents).LoadFrom(Pointer(Raw_));

 aStudent.sStudentId := '296380'; // student actually is in file...

 indx_ := aDynArray.FindHashed(aStudent); // indx_ returns -1
END;

Offline

Board footer

Powered by FluxBB