简体   繁体   English

如何在Delphi中正确释放/完成ActiveX DLL?

[英]How to correctly free/finalize an ActiveX DLL in Delphi?

We are using a class called ODNCServer here - at initialization, an TAutoObjectFactory object is created: 我们在这里使用一个称为ODNCServer的类-在初始化时,将创建一个TAutoObjectFactory对象:

initialization
  pAutoObjectFactory := TAutoObjectFactory.Create(ComServer, TODNCServer, Class_ODNCServer, ciSingleInstance, tmApartment);

Now FastMM is complaining about a memory leak because this object isn't freed anywhere. 现在, FastMM抱怨内存泄漏,因为此对象没有在任何地方释放。 If I add a finalization statement like this 如果我添加这样的终结声明

finalization
  if assigned(pAutoObjectFactory) then
    TAutoObjectFactory(pAutoObjectFactory).Free;

then the object is freed, but after the FastMM dialog about the memory leak pops up, so actually, the OS seems to be unloading the DLL, not the program. 然后释放该对象,但是弹出有关内存泄漏的FastMM对话框后,实际上,操作系统似乎正在卸载DLL,而不是程序。 Instances of ODNCServer are created like this ODNCServer实例是ODNCServer创建的

fODNCServer := TODNCServer.Create(nil);
//register into ROT
OleCheck(
 RegisterActiveObject(
   fODNCServer.DefaultInterface,            // instance
   CLASS_ODNCServer,    // class ID
   ACTIVEOBJECT_STRONG,       //strong registration flag
   fODNCServerGlobalHandle //registration handle result
 ));

and freed like this: 并像这样释放:

if ((assigned(fODNCServer)) and (fODNCServerGlobalHandle <> -1)) then
begin
  Reserved := nil;
  OleCheck(RevokeActiveObject(fODNCServerGlobalHandle,Reserved));
  fDTRODNCServerGlobalHandle := -1;
end;
FreeAndNil(fODNCServer);

So, does anybody know what I have to change to get rid of that memory leak? 那么,有人知道我必须做些什么来摆脱内存泄漏吗? By the way, I also tried using FastMM's RegisterExpectedMemoryLeaks to register and ignore the leak, but this doesn't seem to work. 顺便说一句,我还尝试使用FastMM的RegisterExpectedMemoryLeaks注册并忽略泄漏,但这似乎不起作用。 Additionally, even if, it would just be a workaround and I'd like to know the right way to do this. 此外,即使这只是一种解决方法,我也想知道正确的方法。

Don't worry about it. 不用担心 It's not a "leak" in the strict sense. 从严格意义上讲,这不是“泄漏”。 Yes you are creating an object that is never free'd, but the keyword is "an". 是的,您正在创建一个永不释放的对象,但是关键字为“ an”。 Singular. 单数。

Your application/DLL will not "leak" memory in the sense that it will create numerous instances of these objects,continually increase it's memory use. 您的应用程序/ DLL不会“泄漏”内存,因为它将创建这些对象的大量实例,并不断增加其内存使用量。 Furthermore the memory used by that single factory object (and others like it) will be cleaned up when the process terminates anyway. 此外,当进程终止时,该单个工厂对象(以及其他类似对象)使用的内存将被清除。

If you showed the code you are using to call RegisterExpectedMemoryLeak() it might be possible to determine why it is not working your specific case. 如果显示了用于调用RegisterExpectedMemoryLeak()的代码,则可能可以确定为什么它在您的特定情况下不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM