delphi - TJclStringList 在 Free 上崩溃
问题描述
创建一个简单的 VCL 应用程序:
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
JclStringLists;
var
MyList1: TJclStringList;
MyList2: TJclStringList;
procedure TForm1.FormDestroy(Sender: TObject);
begin
MyList1.Free;
MyList2.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
MyList1 := TJclStringList.Create;
MyList2 := TJclStringList.Create;
MyList1.LoadFromFile('C:\ONE.txt');
MyList2.LoadFromFile('C:\TWO.txt');
Self.Caption := Self.Caption + ' ' + IntToStr(MyList1.Count);
Self.Caption := Self.Caption + ' ' + IntToStr(MyList2.Count);
end;
end.
TForm1.FormDestroy
尝试释放MyList1对象实例时,它在事件处理程序中崩溃。为什么?
解决方案
TJclStringList
是一个引用计数类型(它在JCLStringLists.pas
as中声明type TJclStringList = class(TJclInterfacedStringList, IInterface, IJclStringList)
并同时实现_AddRef
和_Release
处理引用计数),因此您根本不应该将它们创建为对象,也不应该手动释放它们 - 它们会在对它们的引用超出了范围。(这也意味着您不应该将它们声明为全局变量,因为这样您就无法控制它们的生命周期。)
该JclStringLists
单元提供了几个功能,可以为您正确创建接口实例。您可以在该单元中看到它们,就在implementation
关键字上方:
function JclStringList: IJclStringList; overload;
function JclStringListStrings(AStrings: TStrings): IJclStringList; overload;
function JclStringListStrings(const A: array of string): IJclStringList; overload;
function JclStringList(const A: array of const): IJclStringList; overload;
function JclStringList(const AText: string): IJclStringList; overload;
TJclStringList
用来做你想做的事情的正确方法是这样的:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, JCLStringLists;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
MyList1, MyList2: IJCLStringList; // Note I and not T in type.
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
MyList1 := JclStringList;
MyList1.LoadFromFile('C:\Work\Data\FirstName.txt');
MyList2 := JclStringList
MyList2.LoadFromFile('C:\Work\Data\LastName.txt');
// Only to demonstrate that both files got loaded by the code above.
Self.Caption := Format('First: %d Last: %d', [MyList1.Count, MyList2.Count]);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
// Do NOT free the JclStringLists here - they will automatically be released when
// the form is destroyed because the reference count will reach zero (as long as
// you don't have any other references to those variables, which by putting them into
// the private section is unlikely.
end;
end.
推荐阅读
- java - 从引导 2.2.5.RELEASE 迁移到 2.4.6 后 RestTemplate 连接错误
- wordpress - AND 关系 TAX 查询 wordpress 不起作用
- javascript - 正则表达式匹配 key=(value, value) 格式
- python - 为什么 python 不能导入我创建的模块?
- mysql - 2013 年,在 Django 中“在查询期间丢失与 MySQL 服务器的连接”在 MySQL 中执行查询时
- console - 显示不匹配值的控制台日志
- c++ - 在 C++ lamdas 和 QTimer 上。这个警告是什么意思,以及如何摆脱它?
- mysql - 将 DataGridView 中的值与 MySQL 进行比较(如果它们共享相同的 ID)
- javascript - 能够从字符串中提取电子邮件的正则表达式
- angular - 如何在 Angular 11 中重定向外部网站 URL?