我正在向Rad studio 10.2实施 addon (DelphiExpert)。我有计时器,每秒都会打勾。计时器滴答程序计算当前打开的编辑器选项卡,项目名称等的名称和行。当我关闭编辑器选项卡时,工作室有时会失败并出现异常“ TEditSource悬挂引用计数为1 ”
我认为在获得IOTASourceEditor接口后,我还没有计算行数。用户关闭选项卡,工作室想要销毁对应当前IOTASourceEditor的对象。不幸的是,我仍然有接口,所以有一个悬空引用,抛出异常。
是否有更好,更安全的方式来获取此信息(行数,选项卡中的文件名,项目名称)?我想避免这种例外。
计时器事件程序:
procedure TimerEvent(Sender: TObject);
var
activeProject: IOTAProject;
activeEditor: IOTASourceEditor;
dic: TDictionary<string, string>;
begin
activeProject := GetActiveProject;
if(activeProject = nil) then
exit;
activeEditor := GetActiveSourceEditor;
if(activeEditor = Nil) then
exit;
dic := TDictionary<string, string>.Create(10);
dic.Add('project',GetActiveProjectName(activeProject));
dic.Add('lines',IntToStr(CountLinesOfSource(activeEditor)));
有获取接口的函数:
function GetActiveProject: IOTAProject;
var
G: IOTAProjectGroup;
begin
Result := Nil;
G := GetProjectGroup;
if (G <> Nil) AND (G.activeProject <> Nil) then
Result := G.activeProject;
end;
function GetSourceEditor(Module: IOTAModule) : IOTASourceEditor;
Var
iFileCount : Integer;
i : Integer;
Begin
Result := Nil;
If Module = Nil Then Exit;
With Module Do
Begin
iFileCount := GetModuleFileCount;
For i := 0 To iFileCount - 1 Do
If GetModuleFileEditor(i).QueryInterface(IOTASourceEditor,Result) = S_OK Then
Break;
End;
End;
最后,函数计数行并获取名称。
function GetActiveProjectName(activeProject: IOTAProject): string;
var
begin
if (activeProject = Nil) then
begin
Result := '';
exit;
end;
S := activeProject.ProjectOptions.TargetName;
// just parsing the name
end;
Function CountLinesOfSource(SourceEditor: IOTASourceEditor): Integer;
Var
se70: IOTASourceEditor70;
Begin
if (SourceEditor = nil) then
exit(0);
se70 := SourceEditor as IOTASourceEditor70;
exit(se70.GetLinesInBuffer());
End;