(Delphi XE2 update 4)
I'm trying to get a big Microsoft Word OLE automation unit I inherited (based on the early binding TWordApplication and interfaces from the WordXP
/Word2010
units) to close WINWORD.EXE
when all references have been released.
So far, it looks like I did catch a couple of reference leaks: most references are properties or local variables.
Some usage scenario's however still keep WINWORD.EXE
open.
A few of the fixes indicate I should favour local variables in stead of chains from
procedure TOffice_10_XP_WordInterface.AddDocument;
var
WordApplicationDocuments: Documents;
begin
WordApplication_Documents.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
end;
to
procedure TOffice_10_XP_WordInterface.AddDocument;
var
WordApplicationDocuments: Documents;
begin
WordApplicationDocuments := WordApplication_Documents;
WordApplicationDocuments.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam);
end;
based on a WordApplication_Documents property that calls this function:
function TOffice_10_XP_WordInterface.GetWordApplication_Documents: Documents;
begin
Result := WordApplicationReference.Documents;
if not Assigned(Result) then
raise EAccessViolation.Create('Documents');
end;
The properties are there to make the EAccessViolation messages more readable than the $C0000005 errors you get in the debugger.
I'm wondering about generic (since I'll probably need this for other automation projects as well) ways to monitor _AddRef and _Release calls.
I did take a look at these links:
- Understanding and correcting interface reference leaks in Delphi's Vcl.OleCtrls.pas (it does not apply as I don't use the TOleControl based interface)
- How to Find a Missing _Release (that's what I use until now)
- Should the compiler hint/warn when passing object instances directly as const interface parameters? (does not apply: no const parameters that take these interfaces)
- Unused interface reference is not destroyed (fixed in Delphi XE update 1 and above)
- WinWord.exe won't quit after calling Word.Documents.Add - Word .NET Interop
- Is COM broken in XE2, and how might I work around it? (fixed in Delphi XE2 update 2)