2

我有一个使用类的应用程序,一个名为 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

4

2 回答 2

12

你不能调用Self.Free类的析构函数。

免费调用 Destroy 和 Destroy 调用 Free ......直到 Stack Overflow

destructor TBaseDB.Destroy;
begin

  // Don't do that at all in a destructor

  // Self.Free;
  // Self := nil;

  // Always call the parent destructor after running your own code
  inherited;
end;

TObject.Free只是对析构函数的安全调用,因为它会测试Instance是否不为零。

procedure TObject.Free;
begin
  if Self <> nil then
    Destroy;
end;

编辑

关于全局变量PROJ,有一个简单(但不是很明智)的解决方案

destructor TBaseDB.Destroy;
begin

  if Self = PROJ then
    PROJ := nil;

  // Always call the parent destructor after running your own code
  inherited;
end;

你应该看看 Singleton 的实现。

于 2013-03-05T12:51:52.890 回答
2

不要使用:

  Self.Free;
  Self := nil;

在你的析构函数中。

于 2013-03-05T12:52:28.693 回答