Re: Interface Question



Hans-Peter Diettrich wrote:
Rob Kennedy wrote:
So if you free the object by calling Free, you need to be certain that there are no dangling interface references for that object. If there are, then _AddRef or _Release will eventually be called for the object that has already been destroyed, and that's the same problem you have even if you _don't_ "disable" reference counting.

Just _AddRef and _Release cannot cause problems, because the non-counting versions don't access the object. Calling other methods may cause trouble, of course.

In Delphi, an interface reference is a pointer to a field inside an object. When the object gets destroyed, any pointers to its fields become invalid. When the memory gets re-used in another allocation, the memory formerly occupied by the field may get overwritten with something else, so the interface reference is no longer pointing to a pointer to a function table, and thus a call to _AddRef will fail as the program tries to fetch the function's address from the function table.

--
Rob
.