BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to free an interface instance?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design
View previous topic :: View next topic  
Author Message
M.Sameer
Guest





PostPosted: Sun Dec 24, 2006 10:16 pm    Post subject: How to free an interface instance? Reply with quote



Hello everyone,

I just want to make sure that in Win32 I should not call Instance.Free
to free an interface instance. I think I should just nil the pointer
(instance := nil) and Delphi will figure out when to really free the
memory that's when the instance is no more referenced.

Is that correct ?

Thanks in advance,

Mohammed Sameer
Back to top
William Egge
Guest





PostPosted: Sun Dec 24, 2006 10:23 pm    Post subject: Re: How to free an interface instance? Reply with quote



That's correct.

-Bill
http://www.eggcentric.com




"M.Sameer" <msameer1 (AT) hotmail (DOT) com> wrote in message
news:458ea892 (AT) newsgroups (DOT) borland.com...
Quote:
Hello everyone,

I just want to make sure that in Win32 I should not call Instance.Free
to free an interface instance. I think I should just nil the pointer
(instance := nil) and Delphi will figure out when to really free the
memory that's when the instance is no more referenced.

Is that correct ?

Thanks in advance,

Mohammed Sameer
Back to top
Joanna Carter [TeamB]
Guest





PostPosted: Sun Dec 24, 2006 11:18 pm    Post subject: Re: How to free an interface instance? Reply with quote



"M.Sameer" <msameer1 (AT) hotmail (DOT) com> a écrit dans le message de news:
458ea892 (AT) newsgroups (DOT) borland.com...

| I just want to make sure that in Win32 I should not call Instance.Free
| to free an interface instance. I think I should just nil the pointer
| (instance := nil) and Delphi will figure out when to really free the
| memory that's when the instance is no more referenced.

As long as you only hold interface references to an instance, as soon as the
last reference goes out of scope, the instance will be freed; you do not
need to nil references explicitly, unless to leave the reference would
prolong the life of the instance unduly.

Avoid having object references to the same instance as interface references,
unless you really know what you are doing, there are all sorts of reference
counting problems to overcome that way.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Back to top
M.Sameer
Guest





PostPosted: Mon Dec 25, 2006 4:51 pm    Post subject: Re: How to free an interface instance? Reply with quote

Ok, Thank you all for the answers.

It works fine when my class inherits from TInterfacedObject in the test
application.

But when I tried to do the same in the real application where the class
inherits from TDataModule it did not work :(

Why doesn't it work knowing that TDataModule inherits from TComponent
which has an implementation for IInterface/IUnknown ?

Is there a way to work around this issue ?

I tried to create a private instance of TInterfacedObject in my class
and implement IInterface by delegation. But id did not work.

I'm currently trying to copy the code in TInterfacedObject to my class.
Is that right ? any hints/warnings ?

Thanks ,
M.Sameer
Back to top
Bob Dawson
Guest





PostPosted: Mon Dec 25, 2006 9:09 pm    Post subject: Re: How to free an interface instance? Reply with quote

"M.Sameer" wrote
Quote:

Why doesn't it work knowing that TDataModule inherits
from TComponent which has an implementation for
IInterface/IUnknown ?

Because the TComponent implementation sidesteps reference counting
(something it has to do because the linking/ownership system among VCL
TComponent descendents already provides a lifetime manangement system).

Actually this should have been a clue to Borland to make a non-ref-counted
ancestor, rather than refcounting IInterface. Something is very wrong when
the first thing you have to do in an inherited class/interface is to defeat
designed behavior.

Quote:
Is there a way to work around this issue ?

Depending on your other design considerations, you might want to create a
TInterfacedObject descendent that does nothing but hold your datamodule
internally and make it available as a property. That way when the object it
nilled or goes out of scope it will take the DM with it.

But that assumes you're just using the DM in code. It's not so good if you
need to make persistent links to the DM. In that case, just give the DM an
owner and let the Delphi component system take care of lifetime management.

bobD
Back to top
M.Sameer
Guest





PostPosted: Tue Dec 26, 2006 9:11 am    Post subject: Re: How to free an interface instance? Reply with quote

Bob Dawson wrote:
Quote:
"M.Sameer" wrote
Why doesn't it work knowing that TDataModule inherits
from TComponent which has an implementation for
IInterface/IUnknown ?

Because the TComponent implementation sidesteps reference counting
(something it has to do because the linking/ownership system among VCL
TComponent descendents already provides a lifetime manangement system).

I don't see a conflict between the two. If someone holds an object
reference aside with interface reference to the same instance, it would
be his problem. It's like instantiating a TComponent descendant with a
nil owner, u should manage the memory manually and free the object yourself.

Quote:
Actually this should have been a clue to Borland to make a non-ref-counted
ancestor, rather than refcounting IInterface. Something is very wrong when
the first thing you have to do in an inherited class/interface is to defeat
designed behavior.

I think it would be good to make IInterface implemented in the top
most ancestors of VCL classes except TObject.
Also it's much prefrable in design to reference interfaces than classes
so VCL should provide more interfaces with their class hierarchy.

Quote:
Is there a way to work around this issue ?

Depending on your other design considerations, you might want to create a
TInterfacedObject descendent that does nothing but hold your datamodule
internally and make it available as a property. That way when the object it
nilled or goes out of scope it will take the DM with it.

But that assumes you're just using the DM in code. It's not so good if you
need to make persistent links to the DM. In that case, just give the DM an
owner and let the Delphi component system take care of lifetime management.

bobD

Actually that's the case. I only use DM in code.
But i worked it out another way :
I copied the code from TInterfacedObject to my DM and it worked fine.

I had another idea and it worked too. That's to add a function to the
interface called GetObject that would return Self in the classes that
implement the interface.

M.Sameer
Back to top
Joanna Carter [TeamB]
Guest





PostPosted: Tue Dec 26, 2006 5:26 pm    Post subject: Re: How to free an interface instance? Reply with quote

"M.Sameer" <msameer1 (AT) hotmail (DOT) com> a écrit dans le message de news:
4590cfe0 (AT) newsgroups (DOT) borland.com...

| I don't see a conflict between the two. If someone holds an object
| reference aside with interface reference to the same instance, it would
| be his problem. It's like instantiating a TComponent descendant with a
| nil owner, u should manage the memory manually and free the object
yourself.

The difference between TComponent and TInterfacedObject means that what
happens when you have no more interface references changes what happens to
any remaining object references.

| I think it would be good to make IInterface implemented in the top
| most ancestors of VCL classes except TObject.
| Also it's much prefrable in design to reference interfaces than classes
| so VCL should provide more interfaces with their class hierarchy.

The real difference between Delphi's interface management and something
sensible like .NET is that, instead of all references, interface or object,
referring to the same memory address, Delphi holds a list of interfaces at
different adresses. This can cause even more problems than just reference
counting.

Why would you exclude TObject from the ability to implement interfaces ? All
classes should be able to support interfaces but without the problems of
reference counting. Unfortunately, this would then mean using gargage
collection, which would then mean a complete change of the memory model to
support non-deterministic finalisation.

| Actually that's the case. I only use DM in code.
| But i worked it out another way :
| I copied the code from TInterfacedObject to my DM and it worked fine.

If you are only creating data modules in code, then why don't you just use a
class derived from TInterfacedObject istead ? After all, all TDataModule is
is a class that holds components ans that isn't the hardest thing to
arrange.

| I had another idea and it worked too. That's to add a function to the
| interface called GetObject that would return Self in the classes that
| implement the interface.

Beware, that can really screw up reference counting :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Back to top
M.Sameer
Guest





PostPosted: Tue Dec 26, 2006 6:48 pm    Post subject: Re: How to free an interface instance? Reply with quote

Joanna Carter [TeamB] wrote:

Quote:
The real difference between Delphi's interface management and something
sensible like .NET is that, instead of all references, interface or object,
referring to the same memory address, Delphi holds a list of interfaces at
different adresses. This can cause even more problems than just reference
counting.

Is it the difference between Delphi and .Net or between Windows and .Net
Platform ?
AFAIK it's windows who is forcing this behavior on Delphi when dealing
with interfaces. IUnknown is the top most ancestor of all interfaces and
that's because of windows, right?
Can Borland/CodeGear override this ?

Quote:
Why would you exclude TObject from the ability to implement interfaces ?
All classes should be able to support interfaces but without the problems of
reference counting. Unfortunately, this would then mean using gargage
collection, which would then mean a complete change of the memory model to
support non-deterministic finalisation.

Just to make it optional. It would be great of course if all supports
interfaces but i don't know much about the impact of it. So it would be
safe to keep the old behavior existing for 3rd party components that
inherit from TObject.

Quote:
If you are only creating data modules in code, then why don't you just use a
class derived from TInterfacedObject istead ? After all, all TDataModule is
is a class that holds components ans that isn't the hardest thing to
arrange.

DMs have visual components on them.
I used DataModules as a container to ClientDataSets to be able to define
FieldDefs visually instead of code.
So I cannot use TInterfacedObject instead.
But I can use a TInterfacedObject descendant as a container for the DMs.
I'm still considering this idea. It's better Object Orientation but I
have to see the impact on design.

Quote:
| I had another idea and it worked too. That's to add a function to the
| interface called GetObject that would return Self in the classes that
| implement the interface.

Beware, that can really screw up reference counting :-)

Joanna


Thanks very much :)

Mohammed Sameer
Back to top
Joanna Carter [TeamB]
Guest





PostPosted: Tue Dec 26, 2006 7:45 pm    Post subject: Re: How to free an interface instance? Reply with quote

"M.Sameer" <msameer1 (AT) hotmail (DOT) com> a écrit dans le message de news:
45911b00 (AT) newsgroups (DOT) borland.com...

| Is it the difference between Delphi and .Net or between Windows and .Net
| Platform ?

..NET *is* Windows. It is just another API that uses the underlying Windows
core.

| AFAIK it's windows who is forcing this behavior on Delphi when dealing
| with interfaces. IUnknown is the top most ancestor of all interfaces and
| that's because of windows, right?

This is only a Windows standard in that it is a standard for COM. In an
attempt to separatre out interfaces from COM, Delphi introduced IInterface
as a name that implies non-COM but, in reality, is simply a synonym for
IUnknown.

Delphi for .NET does not have a "root" interface name, interfaces are just
that, interfaces. They have no hierarchy above that which a developer
imposes.

| Can Borland/CodeGear override this ?

Sure, but it would break too much legacy code. It's not just a matter of
hierarchy, it would also involve some other way of memory management like
garbage collection.

| DMs have visual components on them.
| I used DataModules as a container to ClientDataSets to be able to define
| FieldDefs visually instead of code.
| So I cannot use TInterfacedObject instead.
| But I can use a TInterfacedObject descendant as a container for the DMs.
| I'm still considering this idea. It's better Object Orientation but I
| have to see the impact on design.

You can always add FieldDefs in code.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Back to top
M.Sameer
Guest





PostPosted: Tue Dec 26, 2006 8:40 pm    Post subject: Re: How to free an interface instance? Reply with quote

Joanna Carter [TeamB] wrote:
Quote:
"M.Sameer" <msameer1 (AT) hotmail (DOT) com> a écrit dans le message de news:
45911b00 (AT) newsgroups (DOT) borland.com...

| Is it the difference between Delphi and .Net or between Windows and .Net
| Platform ?

.NET *is* Windows. It is just another API that uses the underlying Windows
core.

| AFAIK it's windows who is forcing this behavior on Delphi when dealing
| with interfaces. IUnknown is the top most ancestor of all interfaces and
| that's because of windows, right?

This is only a Windows standard in that it is a standard for COM. In an
attempt to separatre out interfaces from COM, Delphi introduced IInterface
as a name that implies non-COM but, in reality, is simply a synonym for
IUnknown.

Delphi for .NET does not have a "root" interface name, interfaces are just
that, interfaces. They have no hierarchy above that which a developer
imposes.

| Can Borland/CodeGear override this ?

Sure, but it would break too much legacy code. It's not just a matter of
hierarchy, it would also involve some other way of memory management like
garbage collection.

I think for garbage collector to exist Borland/CodeGear has to make a
runtime environment for it to run beside the application . If we added
the changes needed to support interfaces in the compiler, Borland has to
make a platform of its own (reinventing the wheel). In other words,
it's impossible to do it directly on Win32 :(

Mohammed Sameer
Back to top
Bob Dawson
Guest





PostPosted: Tue Dec 26, 2006 10:15 pm    Post subject: Re: How to free an interface instance? Reply with quote

"Joanna Carter [TeamB]" wrote

Quote:
| I had another idea and it worked too. That's to add a function to the
| interface called GetObject that would return Self in the classes that
| implement the interface.

Beware, that can really screw up reference counting Smile

And of course it also leads to dependencies on the implementing classes,
thus defeating one on the main justifications for using interfaces at all.

bobD
Back to top
Bob Dawson
Guest





PostPosted: Tue Dec 26, 2006 10:17 pm    Post subject: Re: How to free an interface instance? Reply with quote

"Joanna Carter [TeamB]" wrote


Quote:
| I had another idea and it worked too. That's to add a function to the
| interface called GetObject that would return Self in the classes that
| implement the interface.

Beware, that can really screw up reference counting Smile

and lead to dependencies on the implementing class(es), thus defeating the
main motivation for using an interface in the first place.

bobD
Back to top
Bob Dawson
Guest





PostPosted: Tue Dec 26, 2006 10:27 pm    Post subject: Re: How to free an interface instance? Reply with quote

"M.Sameer" wrote
Quote:

interfaces. IUnknown is the top most ancestor of all interfaces and that's
because of windows, right?
Can Borland/CodeGear override this ?

Actually, Borland chose to make IInterface compatible with COM--that's what
requires the reference counting and the GUID identification mechanism. It
has nothing to do with Windows itself.

Quote:
Just to make it optional. It would be great of course if all supports
interfaces but i don't know much about the impact of it. So it would be
safe to keep the old behavior existing for 3rd party components that
inherit from TObject.

TObject can support interfaces currently--you just have to implement the
refcounting calls yourself.

bobD
Back to top
Bob Dawson
Guest





PostPosted: Tue Dec 26, 2006 10:29 pm    Post subject: Re: How to free an interface instance? Reply with quote

"Joanna Carter [TeamB]" wrote
Quote:

This is only a Windows standard in that it is a standard for COM. In an
attempt to separatre out interfaces from COM, Delphi introduced IInterface
as a name that implies non-COM but, in reality, is simply a synonym for
IUnknown.

Right. Ideally we might have had

ICOMInterface = interface(IInterface)

which would have introduced the required reference counting.

bobD
Back to top
Joanna Carter [TeamB]
Guest





PostPosted: Wed Dec 27, 2006 1:20 am    Post subject: Re: How to free an interface instance? Reply with quote

"Bob Dawson" <RBDawson (AT) prodigy (DOT) net> a écrit dans le message de news:
45914dd2$1 (AT) newsgroups (DOT) borland.com...

| Right. Ideally we might have had
|
| ICOMInterface = interface(IInterface)
|
| which would have introduced the required reference counting.

Ideally, we should have had an interface keyword that has no base type, it
should just *be* an interface, no methods, no nothing, not even a GUID.

Then we could have the base COM interface add a GUID and the appropriate
methods :

IUnknown = interface
[GUID...]
function _AddRef...
function _Release...
function QueryInterface...
end;

But as to how memory management would happen without GC, I am at a loss.

BTW, Hope you had a good Christmas and will have a Happy New Year.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OO design All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.