这段代码的Notification函数是如何被调用的?

ooolinux 2018-02-17 12:31:22
Panel1 = new TPanel((TComponent*)NULL);
Panel1->Parent = this;
Panel1->FreeNotification(this); // <-- this is called automatically when assigning an Owner
...
void __fastcall TForm1::Notification(TComponent *AComponent, TOperation Operation)
{
TForm::Notification(AComponent, Operation);
if (Operation == opRemove)
{
if (AComponent == Panel1) Panel1 = NULL;
else if (AComponent == Image1) Image1 = NULL;
else if (AComponent == Timer1) Timer1 = NULL;
}
}

当delete Panel1时,如何执行到Notification函数的?
Panel1->FreeNotification(this);和TForm1::Notification()函数的关系是什么?
...全文
1406 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ooolinux 2018-02-24
  • 打赏
  • 举报
回复
@早打大打打核战争 明白了,interface或者引入了interface的class默认是强引用,需要弱引用的就用[Weak]修饰。
  • 打赏
  • 举报
回复
[Weak]代表弱引用,指定FOwner不使用引用计数,只是一个纯粹的指针,是为了避免循环引用导致不能释放的问题,只针对interface或者引入了interface的class(比如TComponent ),因为class默认就是弱引用,不使用引用计数,interface是强引用,使用引用计数技术(这话看起来有点别扭)。
ooolinux 2018-02-23
  • 打赏
  • 举报
回复
@早打大打打核战争 好,[Weak] FOwner: TComponent; // [Weak] 是啥意思?
  • 打赏
  • 举报
回复
是其私有成员: TComponent = class(TPersistent, IInterface, IInterfaceComponentReference) private [Weak] FOwner: TComponent; FName: TComponentName; FTag: NativeInt; FComponents: TList<TComponent>; FFreeNotifies: TList<TComponent>; // 这里 FDesignInfo: Longint; FComponentState: TComponentState; FVCLComObject: Pointer; FObservers: TObservers; FOnGetDeltaStreams: TGetDeltaStreamsEvent; 。。。
ooolinux 2018-02-22
  • 打赏
  • 举报
回复
@早打大打打核战争 当delete Panel1时,如何执行到Notification函数呢?
  • 打赏
  • 举报
回复
因为TComponent内部维护一个通知List,Panel1->FreeNotification(this);就把this加入到通知List...
ooolinux 2018-02-22
  • 打赏
  • 举报
回复
@早打大打打核战争 明白了,通知List 是TComponent的哪个属性?
  • 打赏
  • 举报
回复
呃,机器->及其
hongss 2018-02-22
  • 打赏
  • 举报
回复
引用 7 楼 DelphiGuy 的回复:
Panel1->FreeNotification(this);就把this加入到通知List(所有TComponent机器派生类都有),delete Panel1的时候会调用Panel1的析构函数,按通知List里的对象逐一Notification。
学习下
  • 打赏
  • 举报
回复
Panel1->FreeNotification(this);就把this加入到通知List(所有TComponent机器派生类都有),delete Panel1的时候会调用Panel1的析构函数,按通知List里的对象逐一Notification。
ooolinux 2018-02-21
  • 打赏
  • 举报
回复
@tanqth 真理解决问题~
tanqth 2018-02-21
  • 打赏
  • 举报
回复
引用 2 楼 u010165006 的回复:
这边人多点
人多不一定解决问题,重点就不在人多了。
ooolinux 2018-02-17
  • 打赏
  • 举报
回复
这边人多点
lyhoo163 2018-02-17
  • 打赏
  • 举报
回复
帖子帖错地方了。

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧