Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用picturebox处理winforms usercontrol-当前内存泄漏_C#_Winforms_Memory Leaks_User Controls - Fatal编程技术网

C# 使用picturebox处理winforms usercontrol-当前内存泄漏

C# 使用picturebox处理winforms usercontrol-当前内存泄漏,c#,winforms,memory-leaks,user-controls,C#,Winforms,Memory Leaks,User Controls,首先,我读过多个问题和答案,这些问题和答案与我正在讨论的问题相似,但并不相同——我相信这不是重复的 简而言之,我的应用程序正在泄漏内存 我有一个Flowpanel包含一个UserControl的多个实例,其中包含两个Picturebox控件和一个标签以及一个工具提示 My Usercontrol是不可变的,每个实例都包含一些状态信息和来自Properties.Resourcesobject的两个图像 当状态更改时,我从flowpanel中删除usercontrol,并将其替换为新的usercon

首先,我读过多个问题和答案,这些问题和答案与我正在讨论的问题相似,但并不相同——我相信这不是重复的

简而言之,我的应用程序正在泄漏内存

我有一个
Flowpanel
包含一个UserControl的多个实例,其中包含两个
Picturebox
控件和一个
标签以及一个
工具提示

My Usercontrol是不可变的,每个实例都包含一些状态信息和来自
Properties.Resources
object的两个图像

当状态更改时,我从flowpanel中删除usercontrol,并将其替换为新的usercontrol。这导致我的应用程序的内存使用量不断增加

我怀疑我没有以某种方式正确处理usercontrol或picturebox,但不确定在哪里?

UserControl:

public partial class ucArrayStatus : UserControl
{
    private ToolTip toolTip = new ToolTip();
    public ucArrayStatus(string arrayDesc, ArrayStatus status /*this is an enum*/)
    {
        InitializeComponent();
        labArrayDesc.Text = arrayDesc;
        labStatus.Text = status.ToString();
        toolTip.SetToolTip(this, status.ToString());
        if (status == ArrayStatus.Complete)
        {
            //pbStatusIcon is a PictureBox control
            pbStatusIcon.Image = Properties.Resources.complete;
            pbStatusIcon.SizeMode = PictureBoxSizeMode.Zoom;
        }
        else
        {
            pbStatusIcon.Image = Properties.Resources.working;
            pbStatusIcon.SizeMode = PictureBoxSizeMode.CenterImage;
        }
    }
}
public class ArrayUIHandler
{
    //store each usercontrol in a specific order indexed by this dictionary
    private Dictionary<int, ucArrayStatus> configToControl = new Dictionary<int, ucArrayStatus>();
    private void ApplyStatusItem(int index, ArrayStatus status)
    {
        var item = new ucArrayStatus(CreateArrayStatusDescription(), status);
        container.Invoke((Action)(() =>
        {
            lock (accessLock)
            {
                if (!configToControl.ContainsKey(index))
                {
                    Logger.Log("UpdateStatus(): Unable to find array: {0}. ControlCount: {1}", index, 
                                container.Controls.Count);
                }
                var c = configToControl[index]; //extract from dictionary
                var controlIndex = container.Controls.IndexOf(c); //retrieve from flowpanel           

                container.Controls.Remove(c); //remove from flowpanel controlcollection
                configToControl.Remove(index); //remove from dictionary
//dispose (not sure if more is needed here, or to implement dispose in the usercontrol??)
                c.Dispose();
                c = null; //explicitly set to null - not sure if needed?
                container.Controls.Add(item); //add new usercontrol to flowpanel
                container.Controls.SetChildIndex(item, controlIndex); //set index position
                configToControl.Add(index, item); //add to dictionary

            }
        }));
    }
}
我是否需要将Dispose()添加到此类以显式处理映像?

用户控件的创建和删除:

public partial class ucArrayStatus : UserControl
{
    private ToolTip toolTip = new ToolTip();
    public ucArrayStatus(string arrayDesc, ArrayStatus status /*this is an enum*/)
    {
        InitializeComponent();
        labArrayDesc.Text = arrayDesc;
        labStatus.Text = status.ToString();
        toolTip.SetToolTip(this, status.ToString());
        if (status == ArrayStatus.Complete)
        {
            //pbStatusIcon is a PictureBox control
            pbStatusIcon.Image = Properties.Resources.complete;
            pbStatusIcon.SizeMode = PictureBoxSizeMode.Zoom;
        }
        else
        {
            pbStatusIcon.Image = Properties.Resources.working;
            pbStatusIcon.SizeMode = PictureBoxSizeMode.CenterImage;
        }
    }
}
public class ArrayUIHandler
{
    //store each usercontrol in a specific order indexed by this dictionary
    private Dictionary<int, ucArrayStatus> configToControl = new Dictionary<int, ucArrayStatus>();
    private void ApplyStatusItem(int index, ArrayStatus status)
    {
        var item = new ucArrayStatus(CreateArrayStatusDescription(), status);
        container.Invoke((Action)(() =>
        {
            lock (accessLock)
            {
                if (!configToControl.ContainsKey(index))
                {
                    Logger.Log("UpdateStatus(): Unable to find array: {0}. ControlCount: {1}", index, 
                                container.Controls.Count);
                }
                var c = configToControl[index]; //extract from dictionary
                var controlIndex = container.Controls.IndexOf(c); //retrieve from flowpanel           

                container.Controls.Remove(c); //remove from flowpanel controlcollection
                configToControl.Remove(index); //remove from dictionary
//dispose (not sure if more is needed here, or to implement dispose in the usercontrol??)
                c.Dispose();
                c = null; //explicitly set to null - not sure if needed?
                container.Controls.Add(item); //add new usercontrol to flowpanel
                container.Controls.SetChildIndex(item, controlIndex); //set index position
                configToControl.Add(index, item); //add to dictionary

            }
        }));
    }
}

我有一个和你的问题很相似的问题,我把一些事情搞砸了,发现这是可行的

处理图像(我将我的设置作为变量)

pictureBox1Image.Dispose()

pictureBox1.BackgroundImage=null


我建议你先试试,看看是否有帮助。

检查你所有的一次性物品,然后打电话给Dispose查询。我可以看到你有一个一次性的工具提示。请确保处置所有一次性实例。这是一般的最佳实践规则

我目前正在使用此方法运行一个测试,令人烦恼的是,应用程序需要在远程服务器上运行,因此我无法使用visual studio内存调试功能。使用资源对象时是否适用此功能是的,您将资源位图加载到新位图中,并且在使用完后必须将其处理掉。此外,仅处理含有UC的设备是不够的。必须处理您添加的所有位图!