开发者

Delphi: Assigning to global record - global record doesn't change

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 differ开发者_运维技巧ence. The assigned values are lost.

regards,

-Vegar


I guess you have to show a bit more code. With

unit Unit1;

interface

type
  TContext = record
    dummy: Integer;
  end;

var
  context: TContext;

procedure Init(AContext: TContext);

implementation

procedure Init(AContext: TContext);
begin
  context := AContext;
end;

end.

and

program Project1;

{$APPTYPE CONSOLE}

uses
  Unit1 in 'Unit1.pas';

procedure Test;
var
  c: TContext;
begin
  c.dummy := 666;
  Init(c);
end;

begin
  Test;
end.

I get the expected result, i.e. c and Unit1.context both contain 666 after executing Init(c); in Test.


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


Should you not change

procedure Init(AContext: TContext);

to

procedure Init(Var AContext: TContext);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜