<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2722" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>Hi Stacey</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>You should never "free" an interface. </FONT></DIV>
<DIV><FONT face=Arial size=2>Implement reference counting in TMenuItem (see 
TInterfacedObject). The object will be destroyed automatically, when all 
variables that reference it (eg MyMenuItem) go out of scope.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Todd.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV>----- Original Message ----- </DIV>
<BLOCKQUOTE 
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV 
  style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B> 
  <A title=stacey@cjntech.co.nz href="mailto:stacey@cjntech.co.nz">Stacey 
  Verner</A> </DIV>
  <DIV style="FONT: 10pt arial"><B>To:</B> <A title=delphi@ns3.123.co.nz 
  href="mailto:delphi@ns3.123.co.nz">NZ Borland Developers Group - Delphi 
  List</A> </DIV>
  <DIV style="FONT: 10pt arial"><B>Sent:</B> Thursday, September 29, 2005 3:06 
  PM</DIV>
  <DIV style="FONT: 10pt arial"><B>Subject:</B> [DUG] How do you free an Object 
  Instance when you have an InterfaceReference?</DIV>
  <DIV><BR></DIV>
  <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>
  <P>
  <HR>

  <P></P>_______________________________________________<BR>Delphi mailing 
  list<BR>Delphi@ns3.123.co.nz<BR>http://ns3.123.co.nz/mailman/listinfo/delphi<BR>
  <P>
  <HR>

  <P></P>No virus found in this incoming message.<BR>Checked by AVG 
  Anti-Virus.<BR>Version: 7.0.344 / Virus Database: 267.11.8/113 - Release Date: 
  27/09/2005<BR></BLOCKQUOTE></BODY></HTML>