[DUG] How do you free an Object Instance when you have an InterfaceReference?

David Brennan dugdavid at dbsolutions.co.nz
Thu Sep 29 15:23:37 NZST 2005


Carefully ;-).

 

You need to make sure all interface references are out of scope before you
free the object. The best way I found was to implement an interface method
which returns the object as a TWinControl (in my case - TObject would
probably work too). Then do the following code:

 

      LWinControl := LInterface.WinControl;

      LInterface:= nil;

      // Now that the interface reference has been released we can do free. 

      LWinControl.Free;

 

Add try's as necessary for the level of memory protection/error handling
that you require.

 

David. 

 

  _____  

From: delphi-bounces at ns3.123.co.nz [mailto:delphi-bounces at ns3.123.co.nz] On
Behalf Of Stacey Verner
Sent: Thursday, 29 September 2005 3:06 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: [DUG] How do you free an Object Instance when you have an
InterfaceReference?

 

If you have an object the implements and interface, and you have a reference
to the interface, how do you free the object using the reference to the
interface?

 

I have the following (simplified) interface and classes.

 

interface

 

type

 

  IMyMenuItem = interface
    function AddMenuItem(PCaption: String; POnClick: TNotifyEvent):
IMyMenuItem;
    procedure MyFree;
  end;

 

  TMyMenuItem = class(TMenuItem, IMyMenuItem)
  public
    function AddMenuItem(PCaption: String; POnClick: TNotifyEvent):
IMyMenuItem;
    procedure MyFree;
  end;

 

  TMyTBMenuItem = class(TTBItem, IMyMenuItem)
  public
    function AddMenuItem(PCaption: String; POnClick: TNotifyEvent):
IMyMenuItem;
    procedure MyFree;
  end;

 

implementation;

 

function TMyMenuItem.AddMenuItem(PCaption: String; POnClick: TNotifyEvent):
IMyMenuItem;
var
  LMenuItem: TMyMenuItem;
begin
  LMenuItem := TMyMenuItem.Create(PParent.Owner);
  LMenuItem.Caption := PCaption;
  LMenuItem.OnClick := POnClick;
  Result := LMenuItem;
end;

 

procedure TMyMenuItem.MyFree;
begin
  Free;
end;

 

function TMyTBMenuItem.AddMenuItem(PCaption: String; POnClick:
TNotifyEvent): IMyMenuItem;
var
  LMenuItem: TMyTBMenuItem;
begin
  LMenuItem := TMyTBMenuItem.Create(PParent.Owner);
  LMenuItem.Caption := PCaption;
  LMenuItem.OnClick := POnClick;
  Result := LMenuItem;
end;

 

procedure TMyTBMenuItem.MyFree;
begin
  Free;
end;

 

The intention of this is to allow you to dynamically create menu items, but
it doesn't matter if you have a normal menu (TMenuItem) or a
Toolbar 2000 menu (TTBItem). You just deal with IMyMenuItem and the correct
objects are created behind the scenes.

 

This works pretty well except I have an intermittent access violations when
freeing.

 

MyMenuItem: IMyMenuItem;

 

... // Do some stuff.

 

MyMenuItem := RootMenuItem.AddMenuItem('Hello World', HelloWorldClick);

 

... // Later on

 

MMyMenuItem.MyFree; // Intermiitent access violation here!

 

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.

 

Stacey

 

Stacey Verner             Ph:   +64-9-4154790
Software Developer        Fax:  +64-9-4154791
                          DDI:  +64-9-4154797
CJN Technologies Ltd.     Email:  <mailto:stacey at cjntech.co.nz>
stacey at cjntech.co.nz
PO Box 302-278, North Harbour, Auckland 1330, New Zealand
12 Piermark Drive, North Harbour, Auckland, New Zealand
Visit our website at  <http://www.cjntech.co.nz/> http://www.cjntech.co.nz/ 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ns3.123.co.nz/pipermail/delphi/attachments/20050929/46a256ee/attachment-0001.html


More information about the Delphi mailing list