Veela
Veela

Reputation: 280

Access Violation Error when reading TMemoryStream

I'm getting 'Access Violation' Error when reading TMemoryStream.

        with Dataset do
        begin
            Edit;
            FieldByName('FK_ID').AsInteger := aID;
            FieldByName('FINGERNO').AsInteger := aFP.FPIndex;
            FieldByName('FINGERTEMPLATE').AsString := aFP.FPBuffer; {This works fine}
            TBlobField(FieldByName('FINGERTEMPLATE')).LoadFromStream(aFP.FPData); {Access Violation Error}

            ApplyUpdates(0);
        end;

Here is the code that fetches the TMemoryStream (FPData).

type
    TFingerPrint = record
    FPIndex : integer;
    FPData : TMemoryStream;
    FPBuffer : String;
end;

function TMachine.SetFingerPrintStream(
      EmployeeRecord : PEmployeeRecord;  EmployeeFP : PFingerPrint): PEmployeeRecord;
var
    sbuffer: ansistring;
    FPIndex,i : Integer;
    FPList, FPDetailedList, BufferList : TStringList;
    FingerprintStream: TMemoryStream;
    FingerPrint: array of Byte;
    FingerPrintLength: Integer;
    pfilter  : PAnsiChar;
    Adata : String;
    Writer: TBinaryWriter;
begin
    EmployeeRecord.Fingerprints := TList.Create;
    FPList := TStringList.Create;
    FPDetailedList  := TStringList.Create;
    FPDetailedList.StrictDelimiter := True;
    FPDetailedList.Delimiter := ',';

    // BufferList := TStringList.Create;

    pfilter   :=  pAnsiChar(AnsiString('Pin=' + EmployeeRecord.EnrollNumber));
    sbuffer   :=  GetTurnsileDeviceInfo('templatev10','*',pfilter,'');

   // BufferList.Add(sbuffer);
   // BufferList.SaveToFile('C:\BufferText.txt');

    try
        FPList.Text :=  sbuffer;
        for FPIndex := 1 to FPList.Count -1 do
        begin
            FPDetailedList.DelimitedText :=  FPList[FPIndex];

            FingerprintStream := TMemoryStream.Create;
            try
                AData       :=  FPDetailedList[5];

                Assert(AData <> '');
                i := 1;
                while i <= Length(AData) do
                  if AData[i] = #0 then
                    Delete(AData, i, 1)
                  else
                    Inc(i);

                FingerPrintLength := Length(AData);
                SetLength(FingerPrint, FingerPrintLength);
                Writer := TBinaryWriter.Create(FingerprintStream);
                try
                    Writer.Write(TEncoding.Default.GetBytes(AData));
                finally
                    Writer.Free;
                end;

                //FingerprintStream.SaveToFile('C:\StreamTest.txt');

                new(EmployeeFP);
                EmployeeFP.FPIndex := FPIndex -1;
                EmployeeFP.FPData  := FingerprintStream;
                EmployeeFP.FPBuffer := sbuffer;
                EmployeeRecord.Fingerprints.Add(EmployeeFP);
            finally
                FingerprintStream.Free;
            end;
        end;
    finally
        FPList.Free;
        FPDetailedList.Free;
    //    BufferList.Free;
        Result := EmployeeRecord;
    end;
end;

Example of buffer/stream data(BufferText.txt):

SutTUzIxAAADqKsECAUHCc7QAAAbqWkBAAAAg1UiYagaAO0PnADtAHCnwQA0AAsPQQA7qPYPYQBPACgPh6hfAP0PbACkAGWnlgB3AHAPmgB/qF4PSACFACEPZaiVAOQPjwBkAGGn6QCoACsPLwC8qKYPbwDCABkPfajTAOYPpgAkAHmmiADlAPoOJwDkqJwPfADwADQPVKjyAOQPzgAxAJWmQQD5AOwPBQD4qIsNYgAIATcPtKgLAQcNxQDIAYqlXAAcAf4PEwAdqYUPmQAgAcAPR6gpAQAPxADpAYKmqQBDAQQOdgBHqYMOsvvXAVp/dtcfamN7BpoyF9y7cIfK/vIPeImlWn/7AZqqjIMKxtEUam4ItnSfE6SkkH5Vfvb4SIaxqX6BUYYyAW+H5V+vg3+IkYZek6Q/lH4iDi8TAPrNT4Z8PQ7F7hv0gisr8ZaX6Qj0FoIrTApOFq76sPhRpPMByfYR+l/1WqTU+jkPiQpj/blb5PRNhC0BHPU+unMBgYBRfJ9wgiysARkGTQ00Az22YAad/pZ9pwGtqqOHbQdiE/oU5lZYA8L4gIenhS2qgYPZA6Z/whFT6QEgLwEB7d+CBqh4AW3CbgjFwwQkipYIAMQSVYmKowHJHYx1wQTBwtAIAJglesMEZnCkAccljH6Wr8EHqKEpAEQKAF0sdGz8iVoFALv2hpOjAWNO6TDAOvz9av4LAIdd+uz/PJ8TAOdek/8G/cBpwn7BeMJVwgBry2ZvVgUAk7J0wSASAPd4on8G/sI/d4RrDgBjvOL9aPowwP3A/44GA/N9YsDCZQfFXoP2clkQAPOAW23AxMDEwMJJBMWLocyCEwDkpKAFaZNuwMHCwlVKxgDkBCj+FQD3rGHBw2lmiMOWdMCIFQNCvadqwcHBBqTBV8LBTDgLAEXQ3Vf7+vzBwMG0FgNc0qnCSsGMZsLC08H/QBYA9yenbmhzxMKnwYU6Qwyo3ueeSo6xgsAWqPTrp8HBQgSPw27DwcMxSRPF7u4KWsH/wcTDAsLCV8DA/0AJAETv92j6QF0PAMo3l8NowcfFwf/BOzrDpwFb9OltwDj5/8xrBgCp94kGeQe4uQIGNgYQdQSFaJEDEF4JdAUPE5kYA8HDQkA7wcKaCBBiHPf9B/5UagcQniEGVgcKE+Al+v5W//1QDRPgKgNZ/sFaBv38rhE0MgZm/8IQi5MC/8P8xP/JED6XB2vAYMFXyRA74wFUwVJgCtVAVqHC/0Np

I tried to convert the MemoryStream to different encodings(ASCII,UTF8,ANSI) also, but I still get the error.

Upvotes: 1

Views: 747

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

Look at this:

EmployeeFP.FPData  := FingerprintStream;

which is followed by this:

FingerprintStream.Free;

You have destroyed the stream and so FPData refers to an object that has been destroyed. Your runtime error is the consequence of this.

You need to ensure that this stream object lives long enough for all of your uses of it.

Your code is rather long and somewhat involved. There may be other errors but I've not searched for them.

Upvotes: 5

Related Questions