EAccessviolation AV错误,在私有中使用变量 [英] EAccessviolation AV Error using variables in Private

查看:44
本文介绍了EAccessviolation AV错误,在私有中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,如果变量在"Private"(在"Private"(需要限制在本单元内的访问)下定义),我正在整理一些代码.写入它们时出现EAccessViolation错误.如果我只是在实现之前在Var下宽松地定义它们,那么可以正常访问它们.我正在将自己的结构与其他类似的部门进行比较,在这些类似的部门中,私人部门"可以正常使用&无法发现任何重大差异.有什么建议吗?

I'm tidying up some code I've been given, for some reason, if the variables are defined under 'Private' (where they need to be to constrain access within this unit). I get EAccessViolation error when writing to them. If I loosely define them under Var just before implementaion they can be accessed ok. I'm comparing my structure with other similar units, where the Private units work fine & cant spot any significant differences.. Any suggestions?

错误MSG:项目---引发异常类EAccessViolation错误,消息为模块----中的访问振动错误".读取地址0000050F

Error MSG: Project --- raised exception class EAccessViolation Error with message 'access vilation error in module----. Read of address 0000050F

interface
uses
  dialogs, math, dateutils, SysUtils, classes;
type
     //double = extended;   
     TDoubleDoubleArray = array of array of double;
     TSunPositionAlgorithm = class (TObject)
         private
           FLocationChanged: boolean;
         public
           Constructor Create;
           Destructor Destroy;
           procedure SetDefaults;
         end;
  Var
    SunPositionAlgorithm : TSunPositionAlgorithm;
           F_L0: Double;
           F_L1: TDoubleDoubleArray;

implementation
  {TSunPositionAlgorithm }

constructor TSunPositionAlgorithm.Create;
begin
  SetDefaults;
end;

procedure TSunPositionAlgorithm.SetDefaults;
Begin
F_L0:= 1;                   // works ok
  FLocationChanged:=true;  // throws eaccess violation error
End;

调用函数(在David H的第一个问题之后添加到Post中):

Calling function (added to Post after David H's 1st question):

  procedure TSun.NRELPositionOfSun(const DateTime: TDateTime; var Azimuth, Elevation, Declination: double);
  Var

    LSunPositionAlgorithm : TSunPositionAlgorithm;
  Begin
    LSunPositionAlgorithm := TSunPositionAlgorithm.Create;
    Try
      LSunPositionAlgorithm.SetDefaults;

  blah..


    Finally
      LSunPositionAlgorithm.Destroy;
    End;
  End;

推荐答案

您尚未显示调用此代码的代码.但是,很明显,您没有有效的 TSunPositionAlgorithm 对象.

You haven't shown the code that calls this code. However, it's pretty obvious that you don't have a valid TSunPositionAlgorithm object.

获取其中之一:

procedure Test;
var
  spa: TSunPositionAlgorithm;
begin
  spa := TSunPositionAlgorithm.Create;
  try
    spa.SetDefaults;
  finally
    spa.Free;
  end;
end;

您可能有这样的代码:

procedure Test;
var
  spa: TSunPositionAlgorithm;
begin
  spa.SetDefaults;//oops, spa has not been initialised yet  
end;

或可能是这样的:

procedure Test;
var
  spa: TSunPositionAlgorithm;
begin
  spa.Create;//oops, spa has not been initialised yet  
end;

更新,您现在已经显示了调用代码,但显然还没有全部显示,因为问题中的代码未显示您所描述的行为.我要指出的一点仍然是,您必须在某处具有无效的对象引用.我已经展示了几种最常见的实现方法.但是还有其他方法可以获取无效的对象引用.

Update You've now shown the calling code but you clearly haven't shown it all, because the code in the question does not exhibit the behaviour you describe. The point I make remains, you must have an invalid object reference somewhere. I've shown a couple of the most common ways for that to come about. But there are other ways to get an invalid object reference.

问题代码中的另一件事是您的析构函数.必须始终使用 override 指令标记它们.

The other thing wrong with the code in the question is your destructor. They must always be marked with the override directive.

destructor Destroy; override;

必须执行此操作,以便在释放对象时调用析构函数.

You have to do this in order for your destructor to be called when an object is freed.

这篇关于EAccessviolation AV错误,在私有中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆