Uses Unit - Out of memory

3

My TLog class inherits from class TBaseModel:

uses
  BaseModelo;

TNFLog = class(TBaseModelo)
...
end;

But my class TBaseModelo needs to have an attribute of type TLog:

uses
  Log;

TBaseModelo = class(TInterfacedPersistent)
public
  property Log: TLog read FLog write FLog;
end;

When logging into the uses of BaseModelo, when compiling, the error "Out of memory" occurs.

What I understand is a correct loop error? How to get around this?

    
asked by anonymous 28.11.2014 / 14:35

2 answers

2

To get around this you have some alternatives. The most used is to declare both in the same unit, using Foward Declaration

TLog = class;

TBaseModelo = class(TInterfacedPersistent)
public
  property Log: TLog read FLog write FLog;
end;

TLog = class(TBaseModelo)
...
end;

Another alterative, is the one I recommend, is to transform the definition into an interface

unit LogIntf

type
  ILog = interface(IInterface)
  ['{8A897A31-7457-46C3-986A-17786CA11404}']
    procedure Logar(const LogMsg: string);
  end;

From the TBaseModel class you can use:

unit BaseModelo;

uses
  LogIntf;

type
    TBaseModelo = class(TInterfacedPersistent)
    private
      FLog: ILog
    public
      property Log: ILog read FLog write FLog;
    end;

And in the TLog class you can do:

uses
  LogIntf, BaseModelo;

type
    TLog = class(TBaseModelo, ILog)
    ...
    end;
    
29.11.2014 / 20:59
0

This is because you are giving uses in a unit that makes uses of the unit that is giving uses in it that by means of uses of the unit that also makes uses of the unit that is giving uses in her.

Complicated right? Well, that's how Delphi thinks ... Bad, right?

I suggest that you declare TBaseModel and TLog in the same Unit . Delphi is an old gentleman, go slow with it.

    
28.11.2014 / 14:46