当相关类在编译和链接时存在并且在代码中显式存在时,运行时引发的 EClassNotFound 真正意味着什么?

发布于 2024-12-02 20:06:49 字数 1549 浏览 0 评论 0原文

我在表单中的 rtl Streaming 中发生运行时错误,导致在执行 TReader.ReadRootComponent 时引发异常 EClassNotFound。具体的错误消息是“找不到类 TActionList”。

奇怪的是:

  1. 我的主窗体使用操作列表。
  2. 为了好玩,我将 ActnList.pas (来自 VCL 源文件夹)添加到我的项目中,以尝试修复它。

当我实例化几分钟前还在工作的表单时,就会发生这种情况。我所做的更改是在一些子框架代码中:我使用 ifdef 标记删除了其所有实现部分代码,因为我正在模拟一些框架,用于单元测试和原型。

我尝试将操作列表类添加到项目中,并且尝试使用和不使用各种编译器和链接选项,但是,我仍然遇到此异常。显然有什么奇怪的事情发生了。一定还有另一种奇怪的方法来解决这个问题。

事实上,似乎发生了一些非常奇怪的事情。当引发此错误时,我得到以下调用堆栈:

rtl.Classes.ClassNotFound('TActionList')
rtl.Classes.TReader.FindComponentClass(???)
rtl.Classes.FindExistingComponent
rtl.Classes.TReader.ReadComponent(nil)       /// NIL!? WHAT!!!!!
rtl.Classes.TReader.ReadDataInner(???)
rtl.Classes.TReader.ReadData(???)
rtl.Classes.TComponent.ReadState(???)
vcl.Controls.TControl.ReadState(???)
vcl.Controls.TWinControl.ReadState($60B9CF0)
vcl.Forms.TCustomForm.ReadState(???)
rtl.Classes.TReader.ReadRootComponent($606EB90)
rtl.Classes.TStream.ReadComponent($606EB90)
rtl.Classes.InternalReadComponentRes(???,???,$606EB90)
rtl.Classes.InitComponent(TComplexFormContainingFrames)

看来 nil 是故意的,在 TReader.ReadDataInner(Instance:TComponent):

      while not EndOfList do ReadComponent(nil);

更新: 我相信这个问题的答案是理解“正如梅森提到的“序列化上下文”。而且,是时候承认我自己的愚蠢了:我从项目中删除了框架的父级,没有意识到它是框架的父级。我通过将 TMyFrameParent 的类型声明存根为 TMyFrameParent = class(TFrame) 来解决它丢失的问题,这反过来又导致了有问题的情况。我将问题留在这里,因为我认为将来记录在神秘情况下何时发生此异常以及如何修复它可能会非常方便。特别是,梅森有一些关于“序列化上下文”以及它们如何应用于类名查找的非常有趣的信息。

I have a runtime error happening in the rtl Streaming in of a form, causing an exception EClassNotFound to be raised, while doing TReader.ReadRootComponent. The particular error message is "Class not found TActionList".

What is odd is:

  1. My main form uses Action list.
  2. For fun, I added ActnList.pas (from the VCL source folder) to my project, to try to fix it.

This happens to me when instantiating a form that I had working until a few minutes ago. The change that I made was in some sub-frame code: I removed all its implementation section code with an ifdef marker, because I am mocking up some frames, for unit testing and prototypes.

I tried adding the action list class to the project, and I tried with and without various compiler and link options, and yet, I still get this exception. Obviously something weird is up. There must be another weird way to get this problem.

In fact, it seems there is something really weird going on. When this error is raised, I get the following call stack:

rtl.Classes.ClassNotFound('TActionList')
rtl.Classes.TReader.FindComponentClass(???)
rtl.Classes.FindExistingComponent
rtl.Classes.TReader.ReadComponent(nil)       /// NIL!? WHAT!!!!!
rtl.Classes.TReader.ReadDataInner(???)
rtl.Classes.TReader.ReadData(???)
rtl.Classes.TComponent.ReadState(???)
vcl.Controls.TControl.ReadState(???)
vcl.Controls.TWinControl.ReadState($60B9CF0)
vcl.Forms.TCustomForm.ReadState(???)
rtl.Classes.TReader.ReadRootComponent($606EB90)
rtl.Classes.TStream.ReadComponent($606EB90)
rtl.Classes.InternalReadComponentRes(???,???,$606EB90)
rtl.Classes.InitComponent(TComplexFormContainingFrames)

It seems the nil is intentional, in TReader.ReadDataInner(Instance:TComponent):

      while not EndOfList do ReadComponent(nil);

Update: I believe the answer to this question is to understand "serialization contexts" as Mason has mentioned. And, it's time to admit my own Stupidity: I removed the parent of the frame from the project, not realizing it was the parent of the frame. I worked around it being missing by stubbing the type declaration for TMyFrameParent as TMyFrameParent = class(TFrame), and this in turn lead to the condition in question. I am leaving the question here because I think it might be really handy in future to note when this exception occurs in arcane cases, and how to fix it. In particular, Mason has a really interesting bit of information about "serialization contexts" and how they apply to class-name-finding.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

仲春光 2024-12-09 20:06:49

这意味着在当前的反序列化上下文中找不到该类。并非所有现有类都注册用于所有加载。每个表单类都有 RTTI,其中包含对其使用的组件的引用。要使其正常工作,请确保您的表单(或框架,如果这是一个框架)在 private 标记之前声明至少一个 TActionList:

TMyForm = class(TForm)
  ActionList: TActionList;
  OtherComponent: TSomeComponent;
private
  //whatever
public
  //whatever
end;

It means that the class wasn't found in the current deserialization context. Not all existing classes are registered for all loading. Each form class has RTTI containing references to the components it uses. To get this to work, make sure that your form (or frame, if this is a frame) declares at least one TActionList before the private tag:

TMyForm = class(TForm)
  ActionList: TActionList;
  OtherComponent: TSomeComponent;
private
  //whatever
public
  //whatever
end;
浴红衣 2024-12-09 20:06:49

还有另一种方法可以得到此错误:将“public”放在表单定义类的顶部。默认情况下,类成员是“已发布”的。我不小心将“public”添加到表单声明的顶部,它在运行时产生多个“未找到类”异常。

There is another way to get this error: put 'public' at the top of a form definition class. By default class members are 'published'. I accidentally added 'public' to the top of a form declaration and it produces multiple 'Class not found' exceptions at run-time.

月棠 2024-12-09 20:06:49

使用 Classes.RegisterClass 注册您想要在流媒体系统中使用的类。引用来自文档

表单声明中引用的表单类和组件类(实例变量)会自动注册。如果要保存实例,则应用程序使用的任何其他类都必须通过调用 RegisterClass 显式注册。一旦类被注册,它们就可以被组件流系统加载或保存。

Use Classes.RegisterClass to register classes you want to use with the streaming system. Quote from the doc

Form classes and component classes that are referenced in a form declaration (instance variables) are automatically registered. Any other classes used by an application must be explicitly registered by calling RegisterClass if instances are to be saved. Once classes are registered, they can be loaded or saved by the component streaming system.

只有影子陪我不离不弃 2024-12-09 20:06:49

当我的 DFM 文件中存在 TLabel 声明但相应的 PAS 文件中没有该声明时,我收到“EClassNotFound”错误。不知怎的,表单编辑器搞砸了。

直到我从表单中删除除特定“损坏”标签之外的所有标签后,该错误才可见。很难找到它,因为该标签隐藏在面板下面。

一种简单的解决方法是从表单中剪切 (ctrl+x) 该标签(找到它后)并将其粘贴回来。这次表单编辑器将在 PAS 文件中正确插入它的声明。

I got a "EClassNotFound" error when I had a TLabel declaration present in my DFM file but there was no declaration for it in the corresponding PAS file. Somehow the form editor screwed up.

The error was not visible until I deleted all labels from my form except that particular "corrupted" one. It was difficult to hunt it down because that label was hidden under a panel.

One easy fix is to cut (ctrl+x) that label (once you find it) from the form and paste it back. This time the form editor will correctly insert a declaration for it in the PAS file.

轮廓§ 2024-12-09 20:06:49

当您将一个框架从一个项目复制到另一个项目,并且该框架继承自某个项目,并且您伪造继承,但在框架 dfm 中保留“继承的”项目描述时,似乎会发生这种情况,如下所示的项目

inherited ActionList: TActionList
  Left = 520
  Top = 576
end

:结果是梅森谈到的“当前反序列化上下文”,不包含该类。一种修复方法是在上述所有情况下将 Inherited 更改为 object。

It seems that this happens when you copy a frame from one project to another project, and that frame inherits from something, and you fake the inheritance, but leave the "inherited" item descriptions in the frame dfm, items like this:

inherited ActionList: TActionList
  Left = 520
  Top = 576
end

This in turn results in the "current deserialization context" that Mason talked about, not containing the class. One fix is to change Inherited to object in all the above cases.

千笙结 2024-12-09 20:06:49

还有另一种方法可以得到此错误:我将“private”放在表单定义类的顶部(因为没有任何元素在表单之外使用)。

就像之前的答案一样(由 服务器溢出):默认情况下,类成员是“已发布”的,如果您更改表单声明的可见性会在运行时产生多个“未找到类”异常。

There is yet another way to get this error: I have put 'private' at the top of a form definition class (because none of the elements were used outside the form).

So same like in previous answer (by Server Overflow): by default class members are 'published' and if you change visibility of a form declaration it produces multiple 'Class not found' exceptions at run-time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文