Autor Beitrag
Nico80
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

WIN XP
DELPHI 2006
BeitragVerfasst: Di 19.09.06 08:45 
Ich habe folgende Singleton-Klasse

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
type
  TSingleton = class
  strict private
    constructor Create;

  class var
    FInstance:TSingleton;

  public
    class function GetInstance: TSingleton;
  end;

var
  sigleton: TSingleton;

implementation

constructor TSingleton.Create;
begin
  inherited;
end;

class function TSingleton.GetInstance: TSingleton;
begin
  If FInstance = nil Then
  begin
    FInstance := TSingleton.Create();
  end;
  Result := FInstance;
end;

...

singleton := TSingleton.GetInstance;


Nun meine Frage:
Wie gebe ich das Objekt "singleton" korrekt frei?
Reicht es FreeAndNil(singelton) aufzurufen?
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 19.09.06 08:58 
user profile iconNico80 hat folgendes geschrieben:
Reicht es FreeAndNil(singelton) aufzurufen?


Ja :)

AXMD
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 19.09.06 09:04 
Die Frage ist, wieviel Sinn es macht, das bei einem Singleton zu tun. :gruebel: Sinn eines solchen ist es doch gerade, dass man bei GetInstance immer dasselbe Objekt erhält. Wenn Du es freigibst, wird jedesmal ein Neues erzeugt und Du kannst Dir den Singleton-Kram sparen.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 19.09.06 09:09 
Er meint vermutlich am Programmende.

AXMD
Nico80 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

WIN XP
DELPHI 2006
BeitragVerfasst: Di 19.09.06 09:11 
Genau!!
Nico80 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39

WIN XP
DELPHI 2006
BeitragVerfasst: Di 19.09.06 15:19 
Wie sieht's denn mit einem solchen Destruktor aus.
Habe das in einem anderen Besipiel gefunden.
Ist dieser Destruktor überhaupt notwendig?

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
destructor TSingleton.Destroy;
begin
  if Assigned(FInstance) then
    FInstance.Free;
  inherited;
end;