AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

TObjectList.OwnsObjects

Ein Thema von hansmaad · begonnen am 15. Mär 2010 · letzter Beitrag vom 17. Mär 2010
 
hansmaad

Registriert seit: 25. Feb 2010
52 Beiträge
 
Delphi 2010 Professional
 
#1

TObjectList.OwnsObjects

  Alt 15. Mär 2010, 10:03
Halloo,

aus der Dokumentation:

Zitat:
Description

OwnsObjects allows TObjectList to control the memory of its objects. If OwnsObjects is True (the default),

calling Delete or Remove frees the deleted object in addition to removing it from the list.
calling Clear frees all the objects in the list in addition to emptying the list.
calling the destructor frees all the objects in the list in addition to destroying the TObjectList itself.
assigning a new value to an index in Items frees the object that previously occupied that position in the list.
Den letzten Punkt verstehe ich so, dass ich schreiben kann
Delphi-Quellcode:
  L := TObjectList.Create();
  L.Add(Test.Create(1));
  L[0] := Test.Create(2);
  L.Free;
Bei der Zuweisung wird aber kein Destructor aufgerufen und beim Löschen der Liste gibt es eine AccessViolation.
Delphi-Quellcode:
  L := TObjectList.Create();
  L.Add(Test.Create(1));
  L[0] := nil;
  L[0] := Test.Create(2);
  L.Free;
verhindert die Zugriffsverletzung, doch wird A immer noch nicht gelöscht.
Habe ich da was falsch verstanden?
Das nächste Beispiel

Delphi-Quellcode:
  L := TObjectList.Create();
  L.Add(Test.Create(1));
  L[0].Free;
  L[0] := Test.Create(2);
  L.Free;
Hier wird dtor 1 aufgerufen und dann 2mal dtor 2 !? Ein erneuter Afuruf der Funktion führt wieder zu einer Zugriffsverletzung. Einwandfrei funktioniert nur:

Delphi-Quellcode:
  L := TObjectList.Create();
  L.Add(Test.Create(1));
  L[0].Free;
  L[0] := nil;
  L[0] := Test.Create(2);
  L.Free;
Was aber heißt denn dann:
Zitat:
assigning a new value to an index in Items frees the object that previously occupied that position in the list

ps:
gibt es irgendwo Dokumentationen wie TList und andere Standardelemente implementiert sind?

ps2:
Delphi-Quellcode:
constructor test.create(n:Integer);
begin
  inherited create;
  n_ := n;
end;

destructor test.Destroy();
begin
    inherited;
end;
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:34 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz