Discussion:
List of Objects and interface, but with Delphi 6 (don't have latest :( )
(too old to reply)
Brian
2010-10-20 19:23:15 UTC
Permalink
I have Delphi 6, so I can't use the parameterized generics. So, I just
tried stuffing some objects into a list. The objects implement the
IRecyclable interface, but when try to use the code, it gives the
following error.

"Project1.exe raised exception class EPrivilege with message
'Priveleged Instruction'"

It seems that I ought to be able to stuff a list with a set of objects
that implement an interface, cast
each object to that interface and access it. What am I doing wrong?

code based upon the interface example at the following site:
http://www.delphibasics.co.uk/Article.asp?Name=Interface

brian

ListStuff := TList.Create;
// Instantiate our bike and car objects
mumsBike := TBicycle.Create(false, 24);
ListStuff.Add(mumsBike);
dadsCar := TCar.Create('Nissan bluebird');
ListStuff.Add(dadsCar);

for i := 0 to ListStuff.Count - 1 do
if (IRecyclable(ListStuff[i])).isRecyclable then
ShowMessage('Recyclable');
Maarten Wiltink
2010-10-21 09:23:43 UTC
Permalink
Post by Brian
I have Delphi 6, so I can't use the parameterized generics. So, I just
tried stuffing some objects into a list. The objects implement the
IRecyclable interface, but when try to use the code, it gives the
following error.
"Project1.exe raised exception class EPrivilege with message
'Priveleged Instruction'"
It seems that I ought to be able to stuff a list with a set of objects
that implement an interface, cast
each object to that interface and access it. What am I doing wrong?
What's probably happening is that you when you cast the object to an
interface, an interface reference comes into being, and at some point
thereafter goes away again. This causes Magic to happen, in the form
of AddRef and Release calls inserted by the compiler. Since you're
keeping object references but not interface references, the reference
count will drop to zero and the object will destroy itself. So it
should work once, but only once.

There are two ways out of this. The first way is to realise that you
aren't using and don't want the lifetime management implications of
interface reference counting, and disable it in your objects. Return
(-1) from AddRef, and do nothing in Release.

The second way is to not keep object references but only, ever, interface
references. That way, the reference count will only drop to zero if you
really have no references to the object left. Since, as you say, you
don't have a TInterfaceList, you can build it yourself in the same way
the RTL later did. Use a TList and put pointers in it that are really
interface references, taking care to keep the reference counting correct
by hand. When you add a pointer to the list, call AddRef on it; when you
delete it from the list, call Release on it. This can be done either in
the list or in the code that manipulates the list, but it has to happen
every time in either case.

Groetjes,
Maarten Wiltink

Loading...