Delphi:分配给全局记录 - 全局记录不会改变
在delphi单元中,我有一个名为“Context”的全局记录:
interface
type
TContext = record
...
end;
var
context: TContext;
我在这个单元中还有一个初始化过程,采用上下文:
interface
procedure Init(AContext: TContext);
在Init过程中,我尝试将给定的上下文分配给全局上下文:
implementation
procedure Init(AContext: TContext);
begin
context := AContext;
end;
出于某种原因,分配后全局上下文保持为空。这是为什么? 在过程内声明一个局部变量并为其赋值按预期工作。
我应该提到的是,这个单元位于 dll 中,并且 init 过程是从 exe 调用的。 声明一条全局记录或声明几个全局字符串没有什么区别。分配的值丢失。
问候,
-维加
In a delphi unit, I have a global record called 'Context':
interface
type
TContext = record
...
end;
var
context: TContext;
I also have a initialization procedure in this unit, taking a context:
interface
procedure Init(AContext: TContext);
Inside the Init procedure, I try to assign the given context to the global context:
implementation
procedure Init(AContext: TContext);
begin
context := AContext;
end;
For some reason, the global context remains empty after the assignment. Why is that?
Declaring a local variable inside the procedure, and assigning to it works as expected.
What I should have mentioned, is that this unit lives in a dll, and the init procedure is called from the exe.
Declaring a global record, or declaring several global strings makes no difference. The assigned values are lost.
regards,
-Vegar
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你必须展示更多的代码。使用
和
我得到了预期的结果,即在
Test 中执行
。Init(c);
后,c
和Unit1.context
都包含 666I guess you have to show a bit more code. With
and
I get the expected result, i.e.
c
andUnit1.context
both contain 666 after executingInit(c);
inTest
.发现错误了。一切真的有点乱......原来负责调用init方法的对象存在两次,并且包含全局变量的单元同时存在于dll和exe项目中。由于某种原因,调用类的一个实例操纵了 exe 内的全局变量,另一个操纵了 dll 内的全局变量,开发人员和调试器都陷入了停顿......
该代码是一些旧代码的一部分,我们正在尝试分解和清理混乱的遗留代码。我们真的开始掌握“破坏”部分的窍门...
感谢您的回复,很抱歉浪费您的时间。
问候,
-维加
The error is found. Everything was kind of a mess really.... It turned out that the object responsible for calling the init-method existed twice, and the unit containing the global variable existed both inside the dll and the exe project. For some reason, one of the instances of the calling class manipulated the global variable inside the exe and the other the one inside the dll, and both the developer and the debugger where tripped to a halt...
The code is part of some old, messy legacy code which we are trying to break apart and clean up. We really start to get the hang of the 'breaking'-part...
Thanks for the responses, and sorry for wasting your time.
regards,
-Vegar
如果您不更改
procedure Init(AContext: TContext);
到
过程 Init(Var AContext: TContext);
Should you not change
procedure Init(AContext: TContext);
to
procedure Init(Var AContext: TContext);