<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
  <title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
<font size="2"><font face="Courier New">We achieved something kind of
similar by having a TDelayedReleaseManager singleton. Using this, your
menu would remove itself from the parent menu and add itself to the
TDelayedReleaseManager instance which would periodically clean up added
items.<br>
<br>
Phil.<br>
</font></font><br>
Stacey Verner wrote:
<blockquote
 cite="midF2A917BCAC62FA47AE05530061DC3DF014C443@porky.int.cjntech.co.nz"
 type="cite">
  <meta http-equiv="Content-Type" content="text/html; ">
  <meta content="MSHTML 6.00.2900.2722" name="GENERATOR">
  <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>&nbsp;</div>
  <div><span class="831414202-29092005"><font face="Courier New"
 size="2">type</font></span></div>
  <div>&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>&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>&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>&nbsp;</div>
  <div><span class="831414202-29092005"><font face="Courier New"
 size="2">implementation;</font></span></div>
  <div>&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>&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>&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>&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>&nbsp;</div>
  <div><span class="831414202-29092005"><font face="Courier New"
 size="2">... // Do some stuff.</font></span></div>
  <div>&nbsp;</div>
  <div><span class="831414202-29092005"><font face="Courier New"
 size="2">MyMenuItem := RootMenuItem.AddMenuItem('Hello World',
HelloWorldClick);</font></span></div>
  <div>&nbsp;</div>
  <div><span class="831414202-29092005"><font face="Courier New"
 size="2">... // Later on</font></span></div>
  <div>&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>
  <pre wrap="">
<hr size="4" width="90%">
_______________________________________________
Delphi mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Delphi@ns3.123.co.nz">Delphi@ns3.123.co.nz</a>
<a class="moz-txt-link-freetext" href="http://ns3.123.co.nz/mailman/listinfo/delphi">http://ns3.123.co.nz/mailman/listinfo/delphi</a>
  </pre>
</blockquote>
</body>
</html>