如何自由接口实现类? [英] How can free Interface implemented class?

查看:169
本文介绍了如何自由接口实现类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题。因为标题说我想发布一个类实现了一个接口的对象,但是我收到一个错误无效指针操作



接口:

 接口

类型
//接口定义
IMessageEvents = Interface(IInterface)
['{BD27EFC6-CC9A-437A-A8B8-16F722518836}']

过程messageReceived(messageData:String);
End;

实现

我的类:

 键入
TChatManager = Class(TInterfacedObject,IMessageEvents)
Private
受保护
过程messageReceived(messageData:String);超载;
Public
构造函数Create;超载;
Destructor Destroy;覆盖;
过程连接;
过程断开连接;
End;

实现

构造函数TChatManager.Create;
Begin
{.......}
End;

过程TChatManager.connect;
Begin
{.......}
End;

过程TChatManager.disconnect;
Begin
{.......}
End;

过程TChatManager.messageReceived(messageData:String);
Begin
{.......}
End;

Destructor TChatManager.Destroy;
Begin
Inherited Destroy;
End;

我的代码:

  self.chatMng:= TChatManager.Create; 
self.chatMng.Free;

任何人都可以告诉我我做错了什么?提前感谢。

解决方案

似乎 chatMng TChatManager 。这可以通过你分配 TChatManager.Create 到它,并调用 Free 的事实推断出来。 p>

但是, TChatManager 源自 TInterfacedObject 。这意味着它的生命周期由引向它的接口的引用控制。当最后的引用被释放时,对象被销毁。



经验法则是,如果你从 TInterfacedObject派生



修改代码的步骤:


  1. 更改 chatMng IMessageEvents

  2. 删除对 chatMng.Free 的调用,编译器将在任何情况下拒绝。


I have a little problem. As the title says I want to release an object whose class implements an interface, however I get an error "invalid pointer operation".

My Interface:

Interface

Type
  // An interface definition
  IMessageEvents = Interface(IInterface)
    ['{BD27EFC6-CC9A-437A-A8B8-16F722518836}']

    Procedure messageReceived(messageData: String);
  End;

Implementation

My Class:

Type
  TChatManager = Class(TInterfacedObject, IMessageEvents)
  Private
  Protected
    Procedure messageReceived(messageData: String); Overload;
  Public
    Constructor Create; Overload;
    Destructor Destroy; Override;
    Procedure connect;
    Procedure disconnect;
  End;

Implementation

Constructor TChatManager.Create;
Begin
    { ....... }
End;

Procedure TChatManager.connect;
Begin
  { ....... }
End;

Procedure TChatManager.disconnect;
Begin
  { ....... }
End;

Procedure TChatManager.messageReceived(messageData: String);
Begin
  { ....... }
End;

Destructor TChatManager.Destroy;
Begin
  Inherited Destroy;
End;

My Code:

 self.chatMng := TChatManager.Create;
 self.chatMng.Free;

Can anyone tell me what I'm doing wrong? Thanks in advance.

解决方案

It would appear that chatMng is of type TChatManager. That can be deduced by the fact that you assign TChatManager.Create to it, and call Free on it.

However, TChatManager derives from TInterfacedObject. That means that its lifetime is controlled by the references that are taken to its interfaces. When the final reference is released, the object is destroyed.

The rule of thumb is that if you derive from TInterfacedObject then you must never take a reference to the object other than through an interface variable.

Steps to correct your code:

  1. Change chatMng to be of type IMessageEvents.
  2. Remove the call to chatMng.Free which the compiler will object to in any case.

这篇关于如何自由接口实现类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆