[fpc-devel] FreeNotification and opRemove

Martin lazarus at mfriebe.de
Thu Mar 8 10:56:37 CET 2012


I found a behaviour, of which I am not sure, if it is intended. Maybe 
someone can comment

My understanding is that FreeNotification are *always* set up 
bi-directional ?
At least
   C1.FreeNotification(C2);
will set up for C1 to inform C2, and for C2 to inform C1 whichever is 
destroyed first to call the other

And
   C1.RemoveFreeNotification(C2);
will remove both.


   C2.Notification(C1, opRemove);
breaks that.
It will lead to C2 removing C1 from it's list of FreeNotifies, but C1 
will keep C2 on the list.
If then C2 is destroyed, it does not inform C1 about that, and C1 ends 
up with a dangling pointer to the no longer existing C2.
Destroying C2 will then crash.

In the example below (compile with -gt) opRemove is caused by 
Owner.RemoveComponent.
It crashes on the last line.

I was briefly wondering, if in case C1 did call 
"C1.FreeNotification(c2);", then C1 should overwrite Notification and 
check for the opRemove and call "RemoveFreeNotification" itself?
But if that was the case, then why does TComponent interfer with the 
FreeNotify list at all?




program Project1;
{$mode objfpc}{$H+}
uses  Classes;
var
   Owner, C1, C2: TComponent;
begin
   Owner := TComponent.Create(nil);
   C1 := TComponent.Create(Owner);
   C2 := TComponent.Create(Owner);

   C1.FreeNotification(c2);

   Owner.RemoveComponent(C1); // C2 Gets on opRemove
   C2.Free;
   C1.Free; // crash

   ReadLn;
end.




More information about the fpc-devel mailing list