Hello,
Thanks for your help in this matter. I've been speaking
to the programmer of the DLL and he thinks that he may be able to sort
the ref count in his code. If this isn't possible I'll use your
solution.
Its good to see that there are still a couple of people
monitoring this group.
Ian
From: delphigames@yahoogroups.com [mailto:delphigames@yahoogroups.com]
On Behalf Of Grubb, Thomas G. (GSFC-583.0)
Sent: Tuesday, February 10, 2009 7:40 PM
To: delphigames@yahoogroups.com
Subject: RE: [DelphiGames] Casting an object to an interface
Hi Ian,
Then you need to use _AddRef and _Release. This works great but
you need to make sure that you call _Release *before* the listview
deletes its items. Assuming you have a IVector interface and TMyVector
class, the code below works to fill a listbox with interfaces and to be
able to select an item and display the interface somewhere else. When
the form closes, you will see a bunch of "Destroyed XXX" dialogs. If
you comment out the FormClose, the vectors don't get properly destroyed.
Tom
procedure TForm1.FormCreate(Sender: TObject);
var
v: IVector;
i: Integer;
begin
for i := 0 to 5 - 1 do
begin
v := TMyVector.Create;
v.SetXYZ(Random(20)-10, Random(20)-10, Random(20)-10);
v._AddRef;
ListBox1.Items.AddObject(v.ToCommaString, TObject(v));
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
I: Integer;
begin
for I := 0 to ListBox1.Items.Count - 1 do
begin
IVector(Pointer(ListBox1.Items.Objects[i]))._Release;
end;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
if (Sender as TListBox).ItemIndex = -1 then
Exit;
Memo1.Lines.Text :=
IVector(Pointer(TListBox(Sender).Items.Objects[TListBox(Sender).ItemInde
x])).ToCommaString;
end;
{ TMyVector }
destructor TMyVector.Destroy;
begin
ShowMessage('Destroyed '+ToCommaString);
inherited;
end;
-----Original Message-----
From: delphigames@yahoogroups.com <mailto:delphigames%40yahoogroups.com>
[mailto:delphigames@yahoogroups.com
<mailto:delphigames%40yahoogroups.com> ]
On Behalf Of Ian Munro
Sent: Tuesday, February 10, 2009 10:26 AM
To: delphigames@yahoogroups.com <mailto:delphigames%40yahoogroups.com>
Subject: RE: [DelphiGames] Casting an object to an interface
Hello,
I should probably explain my problem in greater detail as using an
interface list will not work for me. I need to populate a visible list
of data (a listview or string grid) that a user can select from. Each
item comes from an enumeration of IJanusDataSourceItem. I wanted to
store each interface reference with each item in a listview control so
that when the user presses a button I can easily grab the appropriate
interface which contains further details. The data in the
IJanusDataSourceItem's can change at runtime so the listview will change
with new data being added or removed as well as the visual textual
content being changed. I have no control over the IJanusDataSourceItem
as these come from a C++ dll. If I use an interface list I then need to
reference that list in my listview, I would also need to maintain the
integrity between the two lists if data was added or removed. I hope
that explains my problem a little better. The key point is that I need
to show some data from the interface in a listview or string grid but
also to be able to maintain a link between the visual content and the
interface to update changes to the interface content etc.
Ian
From: delphigames@yahoogroups.com <mailto:delphigames%40yahoogroups.com>
[mailto:delphigames@yahoogroups.com
<mailto:delphigames%40yahoogroups.com> ]
On Behalf Of Grubb, Thomas G. (GSFC-583.0)
Sent: Tuesday, February 10, 2009 3:17 PM
To: delphigames@yahoogroups.com <mailto:delphigames%40yahoogroups.com>
Subject: RE: [DelphiGames] Casting an object to an interface
BTW, the problem you were having below is that when you shoved the
interface into the Tlist, the reference count was not incremented. So,
at some point, the object behind the interface got freed... And bang
when you tried to get it back. You could have hacked around this by call
__AddRef and all the other reference counting code but it would be a
hack and messy. Much cleaner to use the TinterfaceList (which also has
its own interface, IInterfaceList).
Regards,
Tom
-----Original Message-----
From: delphigames@yahoogroups.com <mailto:delphigames%40yahoogroups.com>
<mailto:delphigames%40yahoogroups.com>
[mailto:delphigames@yahoogroups.com
<mailto:delphigames%40yahoogroups.com>
<mailto:delphigames%40yahoogroups.com> ] On Behalf Of Grubb, Thomas G.
(GSFC-583.0)
Sent: Tuesday, February 10, 2009 10:08 AM
To: delphigames@yahoogroups.com <mailto:delphigames%40yahoogroups.com>
<mailto:delphigames%40yahoogroups.com>
Subject: RE: [DelphiGames] Casting an object to an interface
Hi Ian Munro,
Use a TInterfaceList. That is a TList for interfaces.
Regards,
Tom
-----Original Message-----
From: delphigames@yahoogroups.com <mailto:delphigames%40yahoogroups.com>
<mailto:delphigames%40yahoogroups.com>
[mailto:delphigames@yahoogroups.com
<mailto:delphigames%40yahoogroups.com>
<mailto:delphigames%40yahoogroups.com> ] On Behalf Of Ian Munro
Sent: Tuesday, February 10, 2009 6:39 AM
To: delphigames@yahoogroups.com <mailto:delphigames%40yahoogroups.com>
<mailto:delphigames%40yahoogroups.com>
Subject: [DelphiGames] Casting an object to an interface
Hello,
I need to store references to interfaces in a list so that when the user
selects a list item I can easily access the interface that relates to
that item. I've been casting the interface as a pointer and adding it as
the list items object. Now when I try to retrieve it I try to cast it
back and the compiler moans that you cant cast a TObject to an
interface. I tried casting it to a pointer first and then to the
interface type which the compiler likes (see code below). The problem I
have then is that when the code runs, when the following line runs it
throws an exception. My question is, Is there a better way to do what I
need to do and what is causing the problem with this code. I must say
that I'm not 100% when it comes to interfaces.
Item := IJanusDataSourceItem (pointer (ListItemObject)) ;
This email is intended solely for the person to whom it is addressed and
may contain confidential or legally privileged information. If you are
not the intended recipient, be advised that you have received this email
in error and that any use, dissemination, forwarding, printing or
copying of this email is strictly prohibited. Please notify the author
by replying to this email and destroying all copies of the email and
attachments. Access to this email by anyone else is unauthorised.
Email may be susceptible to data corruption, interception, unauthorised
amendment, viruses and delays or the consequences thereof. Any views or
opinions presented are solely those of the author and do not necessarily
represent those of Grosvenor Technology Ltd.
Grosvenor Technology Ltd. (incorp. Newmark Technology Ltd.) is a company
registered in England with company number 2412554.
The Grosvenor Technology Ltd. Registered Office address is Millars
Three, Southmill Road, Bishop's Stortford, Herts, CM23
[Non-text portions of this message have been removed]
------------------------------------
// *** Yahoo! Groups Info ***
begin
Yahoo! Group := DelphiGames;
HomePage := http://turbo.gamedev.net
Unsubscribe := delphigames-unsubscribe@yahoogroups.com
<mailto:delphigames-unsubscribe%40yahoogroups.com>
<mailto:delphigames-unsubscribe%40yahoogroups.com>
end.Yahoo! Groups Links
------------------------------------
// *** Yahoo! Groups Info ***
begin
Yahoo! Group := DelphiGames;
HomePage := http://turbo.gamedev.net
Unsubscribe := delphigames-unsubscribe@yahoogroups.com
<mailto:delphigames-unsubscribe%40yahoogroups.com>
<mailto:delphigames-unsubscribe%40yahoogroups.com>
end.Yahoo! Groups Links
This email is intended solely for the person to whom it is addressed and
may contain confidential or legally privileged information. If you are
not the intended recipient, be advised that you have received this email
in error and that any use, dissemination, forwarding, printing or
copying of this email is strictly prohibited. Please notify the author
by replying to this email and destroying all copies of the email and
attachments. Access to this email by anyone else is unauthorised.
Email may be susceptible to data corruption, interception, unauthorised
amendment, viruses and delays or the consequences thereof. Any views or
opinions presented are solely those of the author and do not necessarily
represent those of Grosvenor Technology Ltd.
Grosvenor Technology Ltd. (incorp. Newmark Technology Ltd.) is a company
registered in England with company number 2412554.
The Grosvenor Technology Ltd. Registered Office address is Millars
Three, Southmill Road, Bishop's Stortford, Herts, CM23
[Non-text portions of this message have been removed]
------------------------------------
// *** Yahoo! Groups Info ***
begin
Yahoo! Group := DelphiGames;
HomePage := http://turbo.gamedev.net
Unsubscribe := delphigames-unsubscribe@yahoogroups.com
<mailto:delphigames-unsubscribe%40yahoogroups.com>
end.Yahoo! Groups Links
This email is intended solely for the person to whom it is addressed and may
contain confidential or legally privileged information. If you are not the
intended recipient, be advised that you have received this email in error and
that any use, dissemination, forwarding, printing or copying of this email is
strictly prohibited. Please notify the author by replying to this email and
destroying all copies of the email and attachments. Access to this email by
anyone else is unauthorised.
Email may be susceptible to data corruption, interception, unauthorised
amendment, viruses and delays or the consequences thereof. Any views or opinions
presented are solely those of the author and do not necessarily represent those
of Grosvenor Technology Ltd.
Grosvenor Technology Ltd. (incorp. Newmark Technology Ltd.) is a company
registered in England with company number 2412554.
The Grosvenor Technology Ltd. Registered Office address is Millars Three,
Southmill Road, Bishop's Stortford, Herts, CM23
[Non-text portions of this message have been removed]