内存泄漏 [英] memory leaky

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

问题描述

所以我的内存泄漏了,我在C#程序上运行了Code Analysis 并且它说:

So I have a memory leaky and I ran Code Analysis on my C# program a  and it said this :

CA2213 应该处理一次性字段
'Form1'包含IDisposable类型的字段'Form1._serverSocket':'Socket'。更改'Form1'上的Dispose方法,在此字段上调用Dispose或Close。
Reset_pump Form1.Designer.cs
16

CA2213 Disposable fields should be disposed 'Form1' contains field 'Form1._serverSocket' that is of IDisposable type: 'Socket'. Change the Dispose method on 'Form1' to call Dispose or Close on this field. Reset_pump Form1.Designer.cs 16

和然后它"添加了此代码:

and then it "added this code :

  [System.Diagnostics.CodeAnalysis.SuppressMessage(" Microsoft.Usage"," CA2213:DisposableFieldsShouldBeDisposed",MessageId =" _serverSocket" )]

 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "_serverSocket")]

但我不知道该代码的作用...

but I have no idea what that code does ...

这里是它遇到问题的代码:

here is the code it had a problem with :

  /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "_serverSocket")]
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        } 

现在我知道 我有一个内存泄漏,所以这会解决吗?

now I do know  I have a memory leaky, so would this fix it ?

它做了什么?

我写的其他代码:

  private byte[] _buffer = new byte[1024];
        public List<Socket> __ClientSockets { get; set; }
        //List<string> _names = new List<string>();
        private Socket _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      
        public Form1()
        {
            InitializeComponent();
            // This line of code is generated by Data Source Configuration Wizard
           // CheckForIllegalCrossThreadCalls = false;
            __ClientSockets = new List<Socket>();
        }  

可能有用......

is might be useful ...

_serverSocket是一个 全局变量

_serverSocket is a global variable

推荐答案

Hello Btb4198,

Hello Btb4198,

因为套接字是I / O对象,所以在将列表对象设置为null之前应该配置套接字。

Because the socket is I/O object, you should dispose the socket before set the list object as null.

 protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();             
            }

            foreach (Socket socket in this.__ClientSockets)
            {
                socket.Dispose();
            }
            this.__ClientSockets = null;

            base.Dispose(disposing);
        }

祝你好运,

Neil Hu


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

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