我正在接触违规行为'读取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;
以下是获取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;
缓冲区/流数据的示例(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
我试图将MemoryStream转换为不同的编码(ASCII,UTF8,ANSI),但我仍然得到错误。
答案 0 :(得分:5)
看看这个:
EmployeeFP.FPData := FingerprintStream;
之后是:
FingerprintStream.Free;
您已销毁该流,因此FPData
指的是已销毁的对象。您的运行时错误就是这样的结果。
您需要确保此流对象的使用时间足以满足您对它的所有使用。
您的代码相当长且有些参与。可能还有其他错误,但我没有搜索过它们。