[DUG] How do you free an Object Instance
whenyouhaveanInterfaceReference?
David Brennan
dugdavid at dbsolutions.co.nz
Thu Sep 29 17:00:02 NZST 2005
I believe so. It's not particularly nice though... because the underlying
TBaseObject property/getter/setter aren't virtual any extra code you add in
your methods won't be used if some code treats your object as a TBaseObject.
However it will work for the interface because the interface seems to look
for it's methods in the lowest level class which explicitly implements it.
Eg by my understanding your code would work to get IMyInterface to use your
TMyObject GetCaption/SetCaption EVEN if TBaseObject also implemented
IMyInterface (ignoring the missing GetCaption)... That's so long as
TMyObject continues to explicitly implement IMyInterface (if it didn't then
IMyInterface would get stuck referencing the TBaseObject version).
Not relevant I know but an interesting aside.
-----Original Message-----
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 4:42 p.m.
To: NZ Borland Developers Group - Delphi List
Subject: RE: [DUG] How do you free an Object Instance
whenyouhaveanInterfaceReference?
The only reason to not get rid of the TMenu's is that (among others) we
have are few hundred GenList descendants that contain them (with
customisations) so it would take a few days to do it.
So let me get this straight...
The following code should be OK?
TBaseObject = class(TObject)
private
FCaption: String
procedure SetCaption(PCaption: String);
public
property Caption: String read FCaption write SetCaption;
end;
IMyInterface = interface
function GetCaption: String;
procedure SetCaption(PCaption: String): String;
property Caption: String read GetCaption write SetCaption;
end;
TMyObject = class(TBaseObject, IMyInterface)
protected
function GetCaption: String; virtual;
procedure SetCaption(PCaption: String); virtual;
public
property Caption: String read GetCaption write SetCaption;
end;
...
function TMyObject.GetCaption: String;
begin
Result := inherited Caption;
end;
procedure TMyObject.SetCaption(PCaption: String): String;
begin
inherited Caption := PCaption;
end;
Thanks
Stacey
> -----Original Message-----
> From: delphi-bounces at ns3.123.co.nz
> [mailto:delphi-bounces at ns3.123.co.nz] On Behalf Of David Brennan
> Sent: Thursday, 29 September 2005 16:35
> To: 'NZ Borland Developers Group - Delphi List'
> Subject: RE: [DUG] How do you free an Object Instance when
> youhaveanInterfaceReference?
>
> For different reasons we have had to implement wrapper
> GetCaption and SetCaption methods which (amongst other things) do:
>
> Result := inherited Caption;
>
> and
>
> inherited Caption := Value;
>
> Of course that requires the object classes being your own
> descended classes (or at least 3rd party controls were you
> can alter their source). Don't get me started on private
> methods in the VCL. It's like Borland were actually trying to
> be difficult and stop anyone extending anything.
>
> BTW, why don't you just get rid of your remaining TMenuItems
> - problem solved! ;-).
>
> David.
>
>
> -----Original Message-----
> 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 4:00 p.m.
> To: struan.judd at ecargo.co.nz; NZ Borland Developers Group -
> Delphi List
> Subject: RE: [DUG] How do you free an Object Instance when
> you haveanInterfaceReference?
>
> Another question.
>
> All of the classes I am using here have a caption property,
> but they don't gave GetCaption and SetCaption is private.
> FCaption is also private.
>
> Is there any way of settings up an interface with a Caption
> property that will be implemented by the Caption property of
> the actuallt class?
>
> Stacey
>
> > -----Original Message-----
> > From: delphi-bounces at ns3.123.co.nz
> > [mailto:delphi-bounces at ns3.123.co.nz] On Behalf Of Struan Judd
> > Sent: Thursday, 29 September 2005 15:39
> > To: 'NZ Borland Developers Group - Delphi List'
> > Subject: RE: [DUG] How do you free an Object Instance when you have
> > anInterfaceReference?
> >
> > Stacey Verner wrote:
> > > 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: stacey at cjntech.co.nz
> > > <mailto: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/>
> >
> >
> > I'd implement this as a "helper" object that delegates the
> properties,
> > methods and events to the actual obect:
> >
> > Ie.
> >
> > Type
> > TMyMenuItem = class(TObject)
> > public
> > procedure AddMenuItem(PCaption: String; POnClick: TNotifyEvent):
> > TMyMenuItem; virtual; abstract;
> > end;
> >
> > TMyMenuItemStandard = class(TMyMenuItem)
> > private
> > FMenuItem: TMenuItem;
> > public
> > procedure AddMenuItem(PCaption: String; POnClick: TNotifyEvent):
> > TMyMenuItem; override;
> > destructor Destroy;
> > end;
> >
> > .
> > .
> > .
> >
> > And of course Destroy would Free the included Menu item (of
> whatever
> > type)
> >
> > Thus you're not having to mix the Interface variables desire to be
> > reference counted and the standard Delphi wilful
> destruction schemes.
> >
> > TTFN
> >
> > Struan Judd
> > Mailto:struan.judd at ecargo.co.nz Developer
> >
> > Phone: +64 (9) 368 9368 eCargo
>
> > Fax: +64 (9) 368 9369 Visibility - Communication -
> Measurement
> > Mobile: +64 (21) 685 335 Private & Confidential
>
> > http://www.ecargo.co.nz
> >
> >
> > _______________________________________________
> > Delphi mailing list
> > Delphi at ns3.123.co.nz
> > http://ns3.123.co.nz/mailman/listinfo/delphi
> >
>
> _______________________________________________
> Delphi mailing list
> Delphi at ns3.123.co.nz
> http://ns3.123.co.nz/mailman/listinfo/delphi
>
> _______________________________________________
> Delphi mailing list
> Delphi at ns3.123.co.nz
> http://ns3.123.co.nz/mailman/listinfo/delphi
>
_______________________________________________
Delphi mailing list
Delphi at ns3.123.co.nz
http://ns3.123.co.nz/mailman/listinfo/delphi
More information about the Delphi
mailing list