我正在尝试在Tqueue中存储指向记录的指针,然后稍后将指针出列并提取数据,但是我与指针混淆并继续得到“抽象错误”
任何人都可以看到我做错了什么并告诉我正确的解决方案吗?
(顺便说一下,最初我没有^但是后来意识到了我的错误,但却惊讶于它仍然出错了)
该记录包含发送到smtp服务器的电子邮件数据,它使用TstringList来保存正文的每一行,另一行保存每个附件文件名
这是用于存储电子邮件数据的记录结构
TPtrEmailData = ^TEmailDataRec;
TEmailDataRec = record
ToAddr : string; //one email address
CcAddr : string; //addresses delimitated by semicolons
BccAddr : string; //addresses delimitated by semicolons
Subject : String;
Body : TStrings; //each string contains one line of the body
attachments: TStrings;//each string contains a filename
end;
创建我使用的记录
function TFrmSendEmail.CreateNewEmailRec: TPtrEmailData;
var
EmailRecPtr : TPtrEmailData;
begin
new(EmailRecPtr); //make a new record
EmailRecPtr^.Body := Tstrings.Create ;
EmailRecPtr^.attachments := Tstrings.create;
result := EmailRecPtr ;
end;
并在出局后释放它们我使用
procedure TFrmSendSllSmtptEmail.DestroyEmailRec(EmailRecPtr : TPtrEmailData);
//frees memory for the Tstrings and then frees the record
begin
freeandnil(EmailRecPtr^.Body); //free one Tstringlist
FreeAndNil(EmailRecPtr^.attachments); //and the other
FreeAndNil(EmailRecPtr ); //now free the precord pointer
end;
当我使用以下内容将队列中的新记录指针排入队列时,调用CreateNewEmailRec,传入备忘录和列表框,其中包含了人体和附件。这是我收到错误的地方。
procedure TFrmSendEmail.AddToEmailQueue(ToAddr, CCAddr,
BccAddr,Subject:String;
Body: Tmemo; Attachments: TListBox);
var
i : integer;
s : string;
EmailRecPtr : TPtrEmailData;
begin
EmailRecPtr := CreateNewEmailRec; //allocate memory
//deallocated in RemoveFromEmailQueue
EmailRecPtr^.ToAddr := ToAddr;
EmailRecPtr^.CCAddr := CCAddr;
EmailRecPtr^.BccAddr := BccAddr;
for I := 0 to Attachments.Count - 1 do
begin
s := Attachments.Items[i];
EmailRecPtr^.attachments.add(s ); <---- !!! get abstract error here
end;
for I := 0 to Body.lines.Count - 1 do
begin
s := Body.lines[i];
EmailRecPtr^.Body.Add(s) ;
end;
EmailQueue.Enqueue(EmailRecPtr );
end;
当我将指针出列到使用中的数据时,会调用和DestroyEmailRec
procedure TFrmSendEmail.RemoveFromEmailQueue(var ToAddr,
CCAddr,
BccAddr,
Subject: String;
var Body,
Attachments: TStringlist);
var
EmailRecPtr :TPtrEmailData;
i : integer;
s : string;
begin
if EmailQueue.Count > 0 then
begin
Body.Clear;
Attachments.Clear;
EmailRecPtr := EmailQueue.Dequeue; //get pointer to next record
ToAddr := EmailRecPtr^.ToAddr; //populate procedure parameters
CCAddr := EmailRecPtr^.CCAddr;
BccAddr := EmailRecPtr^.BccAddr;
for EmailRecPtr^.attachments.Count - 1 do
begin
s := EmailRec^.attachments[i];
Attachments.Add(s) ;
end;
for I := 0 to EmailRecPtr ^.Body.Count - 1 do
begin
s := EmailRecPtr ^.Body[i];
Body.Add(s);
end;
DestroyEmailRec(EmailRecPtr); //release memory
end;
对RemoveFromEmailQueue的调用传递了几个创建的TStringLists
TheBody := Tstringlist.Create ;
TheAttachments := Tstringlist.create;
try
RemoveFromEmailQueue(ToAddr, CCAddr, BccAddr, Subject,TheBody,TheAttachments);
// do stuff with the data;
finally
TheBody.Free;
TheAttachments.Free;
end;
哦,队列被声明为
var
EmailQueue : Tqueue<TPtrEmailData>;
答案 0 :(得分:3)
你得到了#34;抽象错误&#34;因为你使用了astract对象(TStrings
)!在TFrmSendEmail.CreateNewEmailRec
方法中,将TStrings
替换为TStringList
:
function TFrmSendEmail.CreateNewEmailRec: TPtrEmailData;
begin
new(result); //make a new record
Result^.Body := TStringList.Create ;
Result^.attachments := TStringList.create;
end;
此外,您无法使用FreeAndNil
释放记录!因此,释放记录的方法应该是
procedure TFrmSendSllSmtptEmail.DestroyEmailRec(EmailRecPtr : TPtrEmailData);
//frees memory for the Tstrings and then frees the record
begin
EmailRecPtr^.Body.Free; //free one Tstringlist
EmailRecPtr^.attachments.Free; //and the other
Dispose(EmailRecPtr); //now free the precord pointer
end;