<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.2722" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial size=2><SPAN class=831414202-29092005>If you have an
object the implements and interface, and you have a reference<BR>to the
interface, how do you free the object using the reference to the
interface?</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=831414202-29092005>I have the following
(simplified) interface and classes.</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New"
size=2>interface</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New"
size=2>type</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>
IMyMenuItem = interface<BR> function AddMenuItem(PCaption:
String; POnClick: TNotifyEvent): IMyMenuItem;<BR> procedure
MyFree;<BR> end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>
TMyMenuItem = class(TMenuItem, IMyMenuItem)<BR>
public<BR> function AddMenuItem(PCaption: String; POnClick:
TNotifyEvent): IMyMenuItem;<BR> procedure MyFree;<BR>
end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>
TMyTBMenuItem = class(TTBItem, IMyMenuItem)<BR>
public<BR> function AddMenuItem(PCaption: String; POnClick:
TNotifyEvent): IMyMenuItem;<BR> procedure MyFree;<BR>
end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New"
size=2>implementation;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>function
TMyMenuItem.AddMenuItem(PCaption: String; POnClick: TNotifyEvent):
IMyMenuItem;<BR>var<BR> LMenuItem: TMyMenuItem;<BR>begin<BR>
LMenuItem := TMyMenuItem.Create(PParent.Owner);<BR> LMenuItem.Caption :=
PCaption;<BR> LMenuItem.OnClick := POnClick;<BR> Result :=
LMenuItem;<BR>end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>procedure
TMyMenuItem.MyFree;<BR>begin<BR> Free;<BR>end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>function
TMyTBMenuItem.AddMenuItem(PCaption: String; POnClick: TNotifyEvent):
IMyMenuItem;<BR>var<BR> LMenuItem: TMyTBMenuItem;<BR>begin<BR>
LMenuItem := TMyTBMenuItem.Create(PParent.Owner);<BR> LMenuItem.Caption :=
PCaption;<BR> LMenuItem.OnClick := POnClick;<BR> Result :=
LMenuItem;<BR>end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>procedure
TMyTBMenuItem.MyFree;<BR>begin<BR> Free;<BR>end;</FONT></SPAN></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=831414202-29092005>The intention of
this is to allow you to dynamically create menu items, but<BR>it doesn't matter
if you have a normal menu (TMenuItem) or a<BR>Toolbar 2000 menu (TTBItem). You
just deal with IMyMenuItem and the correct<BR>objects are created behind the
scenes.</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=831414202-29092005>This works pretty
well except I have an intermittent access violations when
freeing.</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>MyMenuItem:
IMyMenuItem;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>... // Do
some stuff.</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>MyMenuItem
:= RootMenuItem.AddMenuItem('Hello World', HelloWorldClick);</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>... // Later
on</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New"
size=2>MMyMenuItem.MyFree; // Intermiitent access violation
here!</FONT></SPAN></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=831414202-29092005></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=831414202-29092005>I don't actually
like the fact that I have MyFree. It was the only way I could think of doing it
at the time.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=831414202-29092005></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN
class=831414202-29092005>Stacey</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV align=left><FONT face="Courier New" size=2>Stacey
Verner
Ph: +64-9-4154790<BR>Software
Developer Fax:
+64-9-4154791<BR>
DDI: +64-9-4154797<BR>CJN Technologies Ltd. Email:
</FONT><A href="mailto:stacey@cjntech.co.nz"><FONT face="Courier New"
size=2>stacey@cjntech.co.nz</FONT></A><BR><FONT face="Courier New" size=2>PO Box
302-278, North Harbour, Auckland 1330, New Zealand<BR>12 Piermark Drive, North
Harbour, Auckland, New Zealand<BR>Visit our website at </FONT><A
href="http://www.cjntech.co.nz/"><FONT face="Courier New"
size=2>http://www.cjntech.co.nz/</FONT></A><FONT face="Courier New" size=2>
</FONT></DIV>
<DIV> </DIV></BODY></HTML>