在Windows服务中处理泄漏 [英] Handle Leaks within a windows service

查看:76
本文介绍了在Windows服务中处理泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用com对象连接到生物识别设备的Windows服务,如下所示,每当执行此代码时,任务管理器中的窗口句柄计数都在增加。任何人都可以帮我解决这个问题。 />


I have a windows service which connects to a biometric device using a com object as shown below, whenever this code gets executed windows handle counts are increasing in the task manager.Can any one help me out to solve this issue.

public CEMClass CtrlComm;

      
       void startPolling()
       {
           CtrlComm = new CEMClass();
           isRunning = true;
           while (isRunning)
           {
               Constants.log.Debug("connecting......");
               if (CtrlComm.Connect_Net("ip", portno))
               {
                   Constants.log.Debug("connected");
               }
               else
               {
                   Constants.log.Debug("not connected");
               }
               Thread.Sleep(2000);
           }
       }

推荐答案

您好,

我的猜测如下:



每次调用startPolling()时都会创建CEMClass类的新实例。它似乎在垃圾收集器的范围内,所以我会说没关系。



但是,CEMClass的内部函数创建的对象不在垃圾收集器的范围内。我很确定,因此没有处理句柄。



CEMClass应具有释放句柄的功能,因为它不是在处理对象时。尝试搜索Dispose_Net或Close等内容。



如果为某些本机代码创建了CEMClass包装器,还应该覆盖destructors~CEMClass和!CEMClass对象处理。
Hello,
my guess is the following:

You create new instances of CEMClass class every time you call startPolling(). It seems that is in scope of garbage collector, so I would say it's OK.

However, the internal functions of CEMClass create objects that are not in scope of garbage collector. I'm quite sure, that handles are not disposed because of this.

CEMClass should have function to release handles, because it's not doing at object disposal. Try search for something like Dispose_Net or Close.

If you created CEMClass wrapper for some native code, you should also override "destructors" ~CEMClass and !CEMClass which are called at object disposal.


没有可见的代码设置 isRunning = false ,所以你的while循环可能永远运行。这与您增加句柄计数的声明相结合,导致假设 startPolling()正在从调用代码异步执行。这是你的意图吗?如果是,那么 CtrlComm 似乎是一个不在 startPolling()范围内的变量是有问题的,因为每个以下调用该方法将覆盖它。否则考虑同步调用方法并让while循环实际上在某些条件下终止(在任何情况下都是后者)。如果 CEMClass 实现IDisposable,请使用 -Statement / Block在中创建实例。
There's no visible code that sets isRunning = false, so your while-loop probably runs forever. That, combined with your statement of increasing handle counts leads to the assumption that startPolling() is being executed asynchronously from the calling code. Is that your intention? If yes, it's problematic that CtrlComm seems to be a variable that is not in the scope of startPolling() because each following call to the method will overwrite it. Otherwise consider calling the method synchronously and let the while-loop actually terminate on some condition (well, the latter in any case). If CEMClass implements IDisposable, create the instance in a using-Statement/Block.


这篇关于在Windows服务中处理泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆