来自堆栈溢出的代码示例泄漏内存,任何人都可以解释原因并提供修复?

时间:2017-02-03 20:44:19

标签: c# memory memory-leaks clipboard

我复制并实施了以下代码。代码完成它应该做的事情。我的问题是它泄漏了记忆。似乎我把它分解为每个方法中线程分配的问题,但我不明白为什么它会泄漏内存。我在循环中调用ContainsText,等待某些文本到达剪贴板。但是我需要修复内存泄漏,任何人都可以解释为什么线程分配正在泄漏,以及如何修复它?

public class ClipboardAsync
{
    private string _GetText;

    private void _thGetText(object format)
    {
        try
        {
            if (format == null)
            {
                _GetText = Clipboard.GetText();
            }
            else
            {
                _GetText = Clipboard.GetText((TextDataFormat)format);
            }
        }
        catch (Exception ex)
        {
            //Throw ex
            _GetText = string.Empty;
        }
    }

    public string GetText()
    {
        string getText = string.Empty;
        ClipboardAsync instance = new ClipboardAsync();
        Thread staThread = new Thread(instance._thGetText);
        staThread.SetApartmentState(ApartmentState.STA);
        staThread.Start();
        staThread.Join();
        getText = instance._GetText;
        return getText;
    }

    public string GetText(TextDataFormat format)
    {
        string getText = string.Empty;
        ClipboardAsync instance = new ClipboardAsync();
        Thread staThread = new Thread(instance._thGetText);
        staThread.SetApartmentState(ApartmentState.STA);
        staThread.Start(format);
        staThread.Join();
        getText = instance._GetText;
        return getText;
    }

    private bool _ContainsText;

    private void _thContainsText(object format)
    {
        try
        {
            if (format == null)
            {
                _ContainsText = Clipboard.ContainsText();
            }
            else
            {
                _ContainsText = Clipboard.ContainsText((TextDataFormat)format);
            }
        }
        catch (Exception ex)
        {
            //Throw ex
            _ContainsText = false;
        }
    }

    public bool ContainsText()
    {
        bool containsText = false;
        ClipboardAsync instance = new ClipboardAsync();
        Thread staThread = new Thread(instance._thContainsText);
        staThread.SetApartmentState(ApartmentState.STA);
        staThread.Start();
        staThread.Join();
        containsText = instance._ContainsText;
        return containsText;
    }

    public bool ContainsText(object format)
    {
        bool containsText = false;
        ClipboardAsync instance = new ClipboardAsync();
        Thread staThread = new Thread(instance._thContainsText);
        staThread.SetApartmentState(ApartmentState.STA);
        staThread.Start(format);
        staThread.Join();
        containsText = instance._ContainsText;
        return containsText;
    }

    private bool _ContainsFileDropList;

    private void _thContainsFileDropList(object format)
    {
        try
        {
            _ContainsFileDropList = Clipboard.ContainsFileDropList();
        }
        catch (Exception ex)
        {
            //Throw ex
            _ContainsFileDropList = false;
        }
    }

    public bool ContainsFileDropList()
    {
        ClipboardAsync instance = new ClipboardAsync();
        Thread staThread = new Thread(instance._thContainsFileDropList);
        staThread.SetApartmentState(ApartmentState.STA);
        staThread.Start();
        staThread.Join();
        return instance._ContainsFileDropList;
    }

    private System.Collections.Specialized.StringCollection _GetFileDropList;

    private void _thGetFileDropList()
    {
        try
        {
            _GetFileDropList = Clipboard.GetFileDropList();
        }
        catch (Exception ex)
        {
            //Throw ex
            _GetFileDropList = null;
        }
    }

    public System.Collections.Specialized.StringCollection GetFileDropList()
    {
        ClipboardAsync instance = new ClipboardAsync();
        Thread staThread = new Thread(instance._thGetFileDropList);
        staThread.SetApartmentState(ApartmentState.STA);
        staThread.Start();
        staThread.Join();
        return instance._GetFileDropList;
    }
}

0 个答案:

没有答案