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 

nil of interface variable to destroy object? (IDispatch)

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation
View previous topic :: View next topic  
Author Message
Neal O'Connell
Guest





PostPosted: Thu Jan 27, 2005 2:27 am    Post subject: nil of interface variable to destroy object? (IDispatch) Reply with quote



I've read that setting an interface variable to nil will destroy the
referenced object. I'm trying to do this, but having odd behavior.

I've learned that the following code does not work for this:

var
Interface1: TInterface1;
begin
Interface1 := CoInterface1.Create;
Interface1.Proc1;
Interface1 := nil;

// Application.ProcessMessages;

Interface1 := CoInterface1.Create;
Interface1.Proc1;
Interface1 := nil;
end;

If I leave the Application.ProcessMessages commented out, I've learned
that the TInterface1 object is not actually destroyed. Uncomment that
line and the object does get destroyed.

A few questions:

1 - is there a better way to force a referenced interface object to
destroy itself?
2 - if not, is it just a fluke that Application.ProcessMessages works
for me? Can I expect this workaround to work all the time on all
computers when I release my application into the wild?


Some details about why I need this to work. I'm working with a pair of
third party COM objects (each is a separate DLL) that both access the
same external data file. From what I can tell, they each seem to load
the contents of the file at creation. At one point I need to change the
contents of the data file through one object and then read them up
through the other.

My problem is that even though I set the 'read' interface variable to
nil after every use, it doesn't appear to get destroyed and the second
call to CoCreate just accesses the initial object (reference counting
it). So the 'read' object still has an old copy of the data file.

On a whim I tossed in an Application.ProcessMessages after setting the
variable to nil to see if that would help and it seemed. But I'm
uncertain if that's a full fix for my problem or just a sloppy hack.

Additional - it doesn't matter if the 'read' object is created twice in
a single procedure or if it is created once in a procedure and that
procedure is called twice. Both give the same bad results.

A third question:

3 - Is it possible that this is more of an issue with unloading the DLL?
If so, how do I wait until the DLL is unloaded?

I'm using D7 Pro Build 7.1 on winXP sp1.

Thanks,
Neal

Back to top
Neal O'Connell
Guest





PostPosted: Fri Jan 28, 2005 9:31 pm    Post subject: Re: nil of interface variable to destroy object? (IDispatch) Reply with quote



Anyone have any suggestions for a different newsgroup to post this question?

Thanks,
Neal

Back to top
Tyro Abecedarian
Guest





PostPosted: Sun Jan 30, 2005 12:31 am    Post subject: Re: nil of interface variable to destroy object? (IDispatch) Reply with quote



"Neal O'Connell" wrote...

[portions elided]
Quote:
If I leave the Application.ProcessMessages commented out, I've learned
that the TInterface1 object is not actually destroyed. Uncomment that
line and the object does get destroyed.
[snip]
1 - is there a better way to force a referenced interface object to
destroy itself?

I'd say that the nil assignment by itself is O.K. My guess is, you're not
doing anything explicit regarding threading model. Assuming that you aren't,
then you're likely using STA (or "Apartment threading" as it was called)
since that is the default. STA threads need a message loop to do some of
their work and part of that work is releasing zero refcount objects.

If you set the CoInitFlags global variable to zero, indicating the desire to
use MTA (or "Free threading"), then that might take care of your release
issue. But, you could still have more issues depending on the
characteristics of the server objects you're using.

Quote:
2 - if not, is it just a fluke that Application.ProcessMessages works
for me? Can I expect this workaround to work all the time on all
computers when I release my application into the wild?

For this purpose, you may decide that using Application.ProcessMessages is
suitable. However, Application.ProcessMessages can be a double-edged sword.
You should probably refrain from using it unless absolutely necessary.
Overuse will make progam behavior somewhat unpredictable since message
processing will become scattered through the various places/layers in your
code.

[snip]
Quote:
3 - Is it possible that this is more of an issue with unloading the DLL?
If so, how do I wait until the DLL is unloaded?

Probably not. If you want to give it a shot then try CoFreeUnusedLibraries.
One warning. To my recollection, calling CoFreeUnusedLibraries does not
cause inproc servers to be unloaded immediately. I think that the COM
subsystem queues the dlls to be freed. Only after some internal timeout are
they actually released.

I hope that helps.



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi OLE Automation All times are GMT
Page 1 of 1

 
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.