We help IT Professionals succeed at work.

TList problem

vbmn
vbmn asked
on
TList problem

How to know if some object in TList is free (destroy, deleted) in application ? Thanks.
Comment
Watch Question

Commented:
The only real way to check (unless you decide to include some sort of Notification method in your class) would be to try and use the Item itself, wrapping the accessor code in a try..except block. Something like this pseudocode ought to do the trick:

var
  oTest: TMyObj;
begin
  oTest := TMyObj(TList.Item[IndexNumber]);

  if Assigned(oTest) then
    try
      oTest.MethodName;
    except
      on E:EAccessViolation do begin
        TList.Item[IndexNumber] := nil;
        TList.Pack;
        end;
    end
  else
    TList.Pack;
end;

The above block will attempt to gain a reference to the object at index IndexNumber (of type TMyObj), if the value of the pointer is non-nil, it will then attempt to do something with it. If the action fails, the exception will set the pointer value to nil. In all cases, if the object at index IndexNumber is unusable, the TList will be packed to remove it's reference from the list.

Hope that helps!

Commented:
Hi vbmn!

I would highly recommend that if you have Delphi5 to make use of TComponentList declared in cntnrs.pas - this is a special list whih is automatically updated when any of the object in the list gets destroyed. However, this list only accept objects of type TComponent and its descendants.

Best regards, Ivo.

Commented:
I strongly suggest AGAINST the method proposed by malsoft, since it is NOT guarateed that you get an access violation when accessing some old memory reference! The memory may already have been assigned to some new object (so that the results would be completely unpredicatble and unreliable!) or the memory could just stuill be buffered by Delphi, since Delphi's memory allocator allocated large block and splits them up internally (so that the memory still belongs to delphi when you would try to access it).

The best solution (most reliable and probably fastest also) is to enhance the destructor of the objects stored in such a way that they delete themselfes from the list when they are freed.

Commented:
vbmn, I don't care who got the points, but please do some testing on your own and find out why the answer you accepted is not suitable to your task in general!

Commented:
Hi vbmn!

In addition to AwonWyss' comment there is another situation in which malsoft's solution may fail...

Assume that oTest object contains a reference to a valid object instance, so the oTest.MethodName method can normally be called. But if this method contains some bad code whose execution leads to EAccessViolation exception, the object reference in the list will be nilled by malsoft's code and the object reference is lost. So you'll get so-called orphan object and also a memory leak.

Btw, have you anything against D5's TComponentList?

Best regards, Ivo.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.