首页 新闻 搜索 专区 学院

关于Delphi接口的使用、释放等问题

0
悬赏园豆:10 [待解决问题]

20120726 

1.       环境

 

Delphi7+Win2003

2.       测试

 

2.1.    测试目标

 

1)  创建一个接口:IInterfaceTest

2)  实现IInterfaceTest接口:TInterfaceTest

3)  创建一个窗体TfrmInterfaceTest,并在窗体中一定一个IInterfaceTest的属性

4)  外部打开TfrmInterfaceTest,后关闭,提示错误

2.1.1.    创建IInterfaceTest

 

IInterfaceTest=interface

    procedure Test();

  end;

2.1.2.    创建TInterfaceTest

 

TInterfaceTest=class(TInterfacedObject,IInterfaceTest)

    procedure Test();

  end;

 

procedure TInterfaceTest.Test;

begin

  ShowMessage('TEST');

end;

2.1.3.    创建TfrmInterfaceTest

 

TfrmInterfaceTest = class(TForm)

    btnInterfaceTest: TButton;

    procedure btnInterfaceTestClick(Sender: TObject);

  private

    FInterfaceTestPart: IInterfaceTest;

    procedure SetInterfaceTestPart(const Value: IInterfaceTest);

    //destructor Destroy; override;

    { Private declarations }

  public

    { Public declarations }

    property InterfaceTestPart:IInterfaceTest read FInterfaceTestPart write SetInterfaceTestPart;

  end;

 

procedure TfrmInterfaceTest.btnInterfaceTestClick(Sender: TObject);

begin

  FInterfaceTestPart.Test;

end;

 

procedure TfrmInterfaceTest.SetInterfaceTestPart(

  const Value: IInterfaceTest);

begin

  FInterfaceTestPart := Value;

end;

2.1.4.    外部调用

 

procedure TfrmMain.btnInterfaceTestClick(Sender: TObject);

var

  LFrm:TfrmInterfaceTest;

  LInterfaceTest:TInterfaceTest;

begin

  LFrm:=TfrmInterfaceTest.Create(nil);

  LInterfaceTest:=TInterfaceTest.Create;

  LFrm.InterfaceTestPart:=LInterfaceTest;

  LFrm.ShowModal;

  LInterfaceTest.Free;

  LFrm.Free;

end;

2.1.5.    问题展现

 

 

关闭frmInterfaceTest窗口的时候,发生异常

3.       完整代码

 1 unit frmInterfaceTestUnit;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls;
 8 
 9 type
10   IInterfaceTest=interface
11     procedure Test();
12   end;
13 
14   TInterfaceTest=class(TInterfacedObject,IInterfaceTest)
15     procedure Test();
16   end;
17 
18   TfrmInterfaceTest = class(TForm)
19     btnInterfaceTest: TButton;
20     procedure btnInterfaceTestClick(Sender: TObject);
21   private
22     FInterfaceTestPart: IInterfaceTest;
23     procedure SetInterfaceTestPart(const Value: IInterfaceTest);
24     { Private declarations }
25   public
26     { Public declarations }
27     property InterfaceTestPart:IInterfaceTest read FInterfaceTestPart write SetInterfaceTestPart;
28   end;
29 
30 var
31   frmInterfaceTest: TfrmInterfaceTest;
32 
33 implementation
34 
35 {$R *.dfm}
36 
37 { TInterfaceTest }
38 
39 procedure TInterfaceTest.Test;
40 begin
41   ShowMessage('TEST');
42 end;
43 
44 procedure TfrmInterfaceTest.btnInterfaceTestClick(Sender: TObject);
45 begin
46   FInterfaceTestPart.Test;
47 end;
48 
49 procedure TfrmInterfaceTest.SetInterfaceTestPart(
50   const Value: IInterfaceTest);
51 begin
52   FInterfaceTestPart := Value;
53 end;
54 
55 end.
56 
57 unit Main;
58 
59 interface
60 
61 uses
62   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
63   Dialogs, StdCtrls,frmInterfaceTestUnit;
64 
65 type
66   TfrmMain = class(TForm)
67     btnInterfaceTest: TButton;
68     procedure btnInterfaceTestClick(Sender: TObject);
69   private
70     { Private declarations }
71   public
72     { Public declarations }
73   end;
74 
75 var
76   frmMain: TfrmMain;
77 
78 implementation
79 
80 {$R *.dfm}
81 
82 procedure TfrmMain.btnInterfaceTestClick(Sender: TObject);
83 var
84   LFrm:TfrmInterfaceTest;
85   LInterfaceTest:TInterfaceTest;
86 begin
87   LFrm:=TfrmInterfaceTest.Create(nil);
88   LInterfaceTest:=TInterfaceTest.Create;
89   LFrm.InterfaceTestPart:=LInterfaceTest;
90   LFrm.ShowModal;
91   LInterfaceTest.Free;
92   LFrm.Free;
93 end;
94 
95 
96 end.
陈凤庆的主页 陈凤庆 | 初学一级 | 园豆:192
提问于:2012-07-26 14:20
< >
分享
所有回答(2)
0

还有用delphi的啊!

Tom.汤 | 园豆:1977 (小虾三级) | 2012-07-26 20:47
0

procedure TfrmMain.btnInterfaceTestClick(Sender: TObject);

var

  LFrm:TfrmInterfaceTest;

  LInterfaceTest:TInterfaceTest;

begin

  LFrm:=TfrmInterfaceTest.Create(nil);

  LInterfaceTest:=TInterfaceTest.Create;

  LFrm.InterfaceTestPart:=LInterfaceTest;

  LFrm.ShowModal;

  //因为你的LFrm是窗体,在showModal关闭后就自动释放掉了,所以下面的

 // LInterfaceTest.Free;   //已经被上面的LFrm释放掉了,这个必须去掉

 // LFrm.Free;  //这个保留是不会报错的,但是也可以不要

end;

呱皮帅哥 | 园豆:202 (菜鸟二级) | 2013-02-21 10:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册