<!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>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=831414202-29092005>I have the following 
(simplified) interface and classes.</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" 
size=2>interface</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" 
size=2>type</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>&nbsp; 
IMyMenuItem = interface<BR>&nbsp;&nbsp;&nbsp; function AddMenuItem(PCaption: 
String; POnClick: TNotifyEvent): IMyMenuItem;<BR>&nbsp;&nbsp;&nbsp; procedure 
MyFree;<BR>&nbsp; end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>&nbsp; 
TMyMenuItem = class(TMenuItem, IMyMenuItem)<BR>&nbsp; 
public<BR>&nbsp;&nbsp;&nbsp; function AddMenuItem(PCaption: String; POnClick: 
TNotifyEvent): IMyMenuItem;<BR>&nbsp;&nbsp;&nbsp; procedure MyFree;<BR>&nbsp; 
end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>&nbsp; 
TMyTBMenuItem = class(TTBItem, IMyMenuItem)<BR>&nbsp; 
public<BR>&nbsp;&nbsp;&nbsp; function AddMenuItem(PCaption: String; POnClick: 
TNotifyEvent): IMyMenuItem;<BR>&nbsp;&nbsp;&nbsp; procedure MyFree;<BR>&nbsp; 
end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" 
size=2>implementation;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>function 
TMyMenuItem.AddMenuItem(PCaption: String; POnClick: TNotifyEvent): 
IMyMenuItem;<BR>var<BR>&nbsp; LMenuItem: TMyMenuItem;<BR>begin<BR>&nbsp; 
LMenuItem := TMyMenuItem.Create(PParent.Owner);<BR>&nbsp; LMenuItem.Caption := 
PCaption;<BR>&nbsp; LMenuItem.OnClick := POnClick;<BR>&nbsp; Result := 
LMenuItem;<BR>end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>procedure 
TMyMenuItem.MyFree;<BR>begin<BR>&nbsp; Free;<BR>end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>function 
TMyTBMenuItem.AddMenuItem(PCaption: String; POnClick: TNotifyEvent): 
IMyMenuItem;<BR>var<BR>&nbsp; LMenuItem: TMyTBMenuItem;<BR>begin<BR>&nbsp; 
LMenuItem := TMyTBMenuItem.Create(PParent.Owner);<BR>&nbsp; LMenuItem.Caption := 
PCaption;<BR>&nbsp; LMenuItem.OnClick := POnClick;<BR>&nbsp; Result := 
LMenuItem;<BR>end;</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2></FONT>&nbsp;</DIV>
<DIV><SPAN class=831414202-29092005><FONT face="Courier New" size=2>procedure 
TMyTBMenuItem.MyFree;<BR>begin<BR>&nbsp; Free;<BR>end;</FONT></SPAN></DIV>
<DIV>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=831414202-29092005>Stacey</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV align=left><FONT face="Courier New" size=2>Stacey 
Verner&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Ph:&nbsp;&nbsp; +64-9-4154790<BR>Software 
Developer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Fax:&nbsp; 
+64-9-4154791<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
DDI:&nbsp; +64-9-4154797<BR>CJN Technologies Ltd.&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;</DIV></BODY></HTML>