Home > Back-end >  Interface automatically release the problem, urgent...
Interface automatically release the problem, urgent...

Time:09-22


//interface declaration
Type
ITestObj=interface
[' {a6b c452d 275-5-49 b5-9527 - C5D1F582A378} ']
The function Gettestid: Integer; Stdcall;
The function Gettestname: string; Stdcall;
Procedure Settestid (const Value: Integer); Stdcall;
Procedure Settestname (const Value: string); Stdcall;
The function testhello: string; Stdcall;
The property testid: Integer read Gettestid write Settestid;
The property testname: string read Gettestname write Settestname;
end;

ITestObjs=interface
[' {3 e517ef1 43 f3 - AC69-390396977-944 - d - b71} ']
The function ByID (nID: Integer) : ITestObj; Stdcall;
Procedure Reset;
end;

IApps=interface
[' {9 c4994e3 - B41A - 47 c6 - AD8E - 8 a2aa23ec3e8} ']
The function TestObjs: ITestObjs; Stdcall;
end;

Var
TestInter: IApps;

//interface implementation
USES the
UtestInterface, Classes, SysUtils;

Type
TTestObj=class (TInterfacedObject ITestObj)
Private
FID: Integer;
FName: string;
The function Gettestid: Integer; Stdcall;
The function Gettestname: string; Stdcall;
Procedure Settestid (const Value: Integer); Stdcall;
Procedure Settestname (const Value: string); Stdcall;
Public
The constructor Create;
The destructor Destroy; Override.
The function testhello: string; Stdcall;
end;

TTestObjs=class (TInterfacedObject ITestObjs)
Private
The List: TList;
Public
The constructor Create;
The destructor Destroy; Override.
The function ByID (nID: Integer) : ITestObj; Stdcall;
Procedure Clear;
Procedure Reset;
end;

TApps=class (TInterfacedObject, IApps)
Private
_ITestObjs: ITestObjs;
Public
The function TestObjs: ITestObjs; Stdcall;
end;

Concrete realization method//
Function TTestObjs. ByID (nID: Integer) : ITestObj;
Var
I: Integer;
TestObj: TTestObj;
The begin
Result:=nil;
If not Assigned (List) then
The Exit;
For I:=0 to the List. Do the Count - 1
The begin
TestObj:=TTestObj (the List [I]);
If Assigned (TestObj) and (TestObj Gettestid=nID) then
The begin
Result:=TestObj;
Break;
end;
end;
//TODO - Eternally: TTestObjs ByID default body inserted
end;

Procedure TTestObjs. Reset;
Var
I: Integer;
TestObj: TTestObj;
The begin
If not Assigned (List) then
List:=TList. Create;
The Clear;
For I:=1 to 5 do
The begin
TestObj:=TTestObj. Create;
TestObj. Settestid (I);
TestObj. Settestname (' TestObj + IntToStr (I));
List. The Add (TestObj);
end;
//TODO - Eternally: TTestObjs. Reset the default body inserted
end;

//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//total interface entry
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
The function _WZInterface: IApps;

Var
_TestObjs: TTestObjs;

Implementation

Var
_testClasses: TApps;

//call
Procedure TForm1. Button1Click (Sender: TObject);
Var
NID: Integer;
Str: string;
The begin
NID:=2;
Str:=TestInter. TestObjs. ByID (nID) testname;
ShowMessage (Str);
//call out after the button event ByID interface is called TTestObj Destroy release, why call Destroy release? The absence of manual release
//TODO - Eternally: TForm1. Button1Click default body inserted
end;

/call out after the button event ByID interface is called TTestObj Destroy release, why call Destroy release? The absence of manual release

CodePudding user response:

This is a typical interface model and object model caused by the mixture of pit, congratulations ~
into the pit
Is retained in the List object references, then the reference counting mechanism of the interface does not work,
When called on the ByID interface reference mechanism + 1 at the beginning, after the event handling - 1, the result is 0
So, the corresponding objects are destroyed,

The correct way is: should be reserved interface references in the List, is the Create object after
Immediately using only the interface types of variables for processing,,,