AV 当使用一个组件中的过程被另一组件调用时

发布于 2024-07-15 12:39:52 字数 1263 浏览 3 评论 0原文

我不确定我是否已尽我所能解释这一点,但是,我们开始...

我在表单上有 2 个自定义组件,它们在设计时通过 IDE 链接在一起。 每当我从组件调用过程时,我都会遇到访问冲突,

地址 0049A614 处的访问冲突 在模块“Project2.exe”中。 读过 地址00000034。

这是我代码的一小部分

TMyClient = class(TClientSocket)
{...}
end;

,...

TPresence = class(TComponent) 
 private
  ftheClient: TMyClient 
 public 
  procedure SetStatus(status: string);

 published
  property UserName : string read fUserName write fUserName;
  property theClient: TMyClient read ftheClient write ftheClient;
 end;   



procedure TPresence.SetStatus(status: string);
begin
 try
*****   if theClient = nil then
     Exception.Create('theClient  is Nil');
 except
   on e:Exception do
   MessageDlg(e.classname+', '+e.message, mtWarning, [mbOK], 0);
 end;
  {...}
end;

0049A614 位于 *****,IDE 停在这里。

我还尝试在运行时进行分配,但

Presence1.theClient := MyClient1;

没有成功,

使用来自 Presence1 或 MyClient1 的程序,这些程序彼此不依赖,工作正常。

德尔福7

后续: 从 mghie 的评论中,我重新考虑了这一点。

我从表单中删除了 TPresence 组件(这导致了一些奇怪的 IDE 错误,这可能与它有关)并在设计时创建了它,分配了所需的所有内容。 现在它可以工作了,但是将 TPresence 组件放回原处会再次出现错误。

谢谢你们的帮助,如果我不能重新打开另一个问题,我现在应该能够解决这个问题了:)

Im not sure if i have explaned this the best i can but, here we go...

I have 2 Custom components on a form, Which are link together at design time through the IDE. Whenever i call a procedure from on of the Component i get the Access violation,

Access violation at address 0049A614
in module 'Project2.exe'. Read of
address 00000034.

This is a small section of my code

TMyClient = class(TClientSocket)
{...}
end;

and...

TPresence = class(TComponent) 
 private
  ftheClient: TMyClient 
 public 
  procedure SetStatus(status: string);

 published
  property UserName : string read fUserName write fUserName;
  property theClient: TMyClient read ftheClient write ftheClient;
 end;   



procedure TPresence.SetStatus(status: string);
begin
 try
*****   if theClient = nil then
     Exception.Create('theClient  is Nil');
 except
   on e:Exception do
   MessageDlg(e.classname+', '+e.message, mtWarning, [mbOK], 0);
 end;
  {...}
end;

0049A614 is at the *****, and the IDE stops here.

I Have also tried to do the assign at run time with

Presence1.theClient := MyClient1;

with no luck

using procedures from Presence1 or MyClient1 that do not rely on each other work fine.

Delphi 7

Follow Up:
from mghie comments, i rethought about it.

I removed the TPresence Component from the form (which caused some strange IDE errors, that might have had something to do with it) and created it design time, assigning everything that was needed. Now it works, but putting the TPresence Component back on the from brings the error back.

Thankyou for your help guys, i should be able to work this one out now, if i can't ill reopen another question :)

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

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

发布评论

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

评论(2

面犯桃花 2024-07-22 12:39:52

您似乎认为引发异常是因为 Presence1 的 client 字段未设置 - 如果您确实收到异常“Read of address 00000034”,则意味着 SetStatus() 中的 Self 指针> 调用nil。 这表明您对未分配的 TPresence 引用调用 SetStatus()。 从您发布的代码片段中实际上不可能说出其原因,但它应该让您开始调试。

我仍然建议您为自己的自定义组件中的所有组件引用编写适当的 setter 方法 - 首先是因为在调试此类问题时您有更好的钩子(您可以在那里设置断点),其次是因为您应该始终调用TComponent.FreeNotification() 能够跟踪其破坏并将内部引用设置为 nil

You seem to be thinking that the exception is raised because the client field of Presence1 is not set - if you do however get the exception "Read of address 00000034" it means that the Self pointer in the SetStatus() call is nil. That would indicate that you call SetStatus() on an unassigned TPresence reference. It is not really possible to tell the reason for that from the snippet you posted, but it should get you started debugging.

I would still advise you to write a proper setter method for all component references in your own custom components - first because you have a better hook when debugging such problems (you can set a breakpoint there), and second because you should always call TComponent.FreeNotification() on such linked components to be able to track their destruction and set the internal reference to nil.

兮子 2024-07-22 12:39:52

我们可能需要您的更多代码。 您可能没有正确创建 TPresence 实例,这会导致您遇到错误。 尝试给我们一个尽可能简单的导致您的错误的代码片段。

We probably need more of your code. It is possible you are not correctly creating an instance of TPresence which would give you the error you are experiencing. Try to give us a simple as possible code snippet that causes your error.

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