我有一个使用类的应用程序,一个名为 TBaseDB 的基类,并且会有许多 TBaseDB 的后代,所有 DIRECT 兄弟姐妹,现在只有一个已经启动,TOraDB,但稍后会添加 TSQLDB 和其他。
我的应用程序使用该类的一个实例,它是一个全局实例,即一个名为 PROJ 的全局变量。我对构造函数、析构函数和全局变量的理解存在问题,导致应用程序中的其他地方出现 EStackOverflow。如果我注释掉我的 PROJ.CREATE,EStackOverflow 就会消失。
我的构造函数只设置变量,它们不会动态创建链表、数组或其他内存密集型对象。
以下是一些代码片段。
// global var definition
// Specifically of BASE class, so I can call any child class without knowing which child class it is...
PROJ : TBaseDB;
我的例程导致我的错误......
procedure TForm1.ShowBug;
begin
// We have clicked on 'Create New Oracle Project
// Now create our PROJ object.
// This is defined globally, but MAY have been used before
// so 'zero' it out
FreeAndNil(PROJ);
// Note that the create is on the CHILD class, not the BASE class
// If I DON'T create the class, no error....
PROJ := TOraDB.Create;
// ... other code
end;
这是我的类定义。
Type
TBaseDB = class
published
public
DAC_ID: Integer;
DAC_ShortName : String;
DAC_LongName: String;
Constructor Create;
Destructor Destroy; override;
... other stuff
end;
implementation
// TBaseDB /////////////////////////////////////////////////////////////////
constructor TBaseDB.Create;
begin
inherited;
end;
destructor TBaseDB.Destroy;
begin
// If I comment out next two lines, my issue goes away
// but shouldn't I have them....?
Self.Free;
Self := nil;
// Always call the parent destructor after running your own code
inherited;
end;
这是我对 TOraDB 类的定义
Type
TOraDB = Class(TBaseDB)
public
Constructor Create;
Destructor Destroy; override;
... other stuff
End;
implementation
// ------------------------------------------------------------------------------
constructor TOraDB.Create;
begin
inherited;
// Now set up the information about the source database. We know it is Oracle
// even though we DONT know if it is connected
DAC_ID := 4;
DAC_ShortName := 'Ora';
DAC_LongName := 'Oracle';
end;
// -----------------------------------------------------------------------------
destructor TOraDB.Destroy;
begin
// Always call the parent destructor after running your own code
inherited;
end;
我不了解有关“重置”全局类变量的内容。我应该在哪里'重置它,所以我仍然可以使用 GLOBAL 变量 PROJ?
谢谢,
GS