Einzelnen Beitrag anzeigen

Benutzerbild von Stevie
Stevie

Registriert seit: 12. Aug 2003
Ort: Soest
4.007 Beiträge
 
Delphi 10.1 Berlin Enterprise
 
#4

AW: [Spring-DI] MemoryLeak bei Einsatz von DelegatedConstructor

  Alt 1. Feb 2012, 14:54
Zitat:
What happens is that a local variable is added to the current routine which contains the address of the method or function pointer, and then this local variable is captured by a hidden anonymous method. This hidden anonymous method is kept alive for the duration of the current routine by a second local variable, of type IInterface (this prevents premature disposal).

The trouble is that with unit initialiaztion, and program begin/end, and similar top-level blocks, "local" variables are in fact global variables.
D.h. das "object behind the scene" (dcWallpaper$43200$ActRec), welches hinter der anonymen Methode sitzt (wir erinnern uns: eine Anonyme Methode ist eigentlich nix anderes als eine Interface Methode mit nen bisschen compiler magic) wird im Falle der Benutzung in initialization (auch in finalization oder in der "main" der dpr) in der globalen Variable gespeichert. Lagerst du aber den Code, welchen du zuvor direkt unter initialization hattest in eine Routine aus, wird die Variable lokal in der Routine angelegt. Somit ist sie nicht mehr global, da sie einen kleineren Scope hat (nämlich den der Routine) und du hast auch kein Memleak mehr.

Das hat imho überhaupt nichts mit der Verwaltung des vom DI Container erzeugten Objekts zu tun.
Stefan
“Simplicity, carried to the extreme, becomes elegance.” Jon Franklin

Delphi Sorcery - DSharp - Spring4D - TestInsight
  Mit Zitat antworten Zitat