简体   繁体   中英

CA2000: Dispose object warning

I have the following method:

    public byte[] HtmlToDoc(string hmtl, string userId)
    {
        byte[] data;
        var auditor = new ServiceAuditor
        {
            User = userId
        };
        try
        {
            using (var tx = new ServerText())
            {
                tx.Create();
                tx.Load(Server.HtmlDecode(hmtl), StringStreamType.HTMLFormat);
                tx.Save(out data, BinaryStreamType.MSWord);
            }
        }
        catch (Exception e)
        {
            auditor.Errormessage = e.Message + "/n " + e.StackTrace;
            data = new byte[0];
        }
        finally
        {
            auditor.Save();
            auditor.Dispose();
        }
        return data;
    }

and I receive the following warning during compilation:

warning CA2000: Microsoft.Reliability : In method 'DocCreator.HtmlToDoc(string, string)', object 'new ServiceAuditor()' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'new ServiceAuditor()' before all references to it are out of scope.

The weird thing is that I don't see why it is complaining even though I am disposing the object. Could you point where is the issue?

The issue you have is this line:

auditor.Save();

If that throws an exception, the next line won't run which is responsible for disposing your auditor object. So you could wrap the Save call in another try / catch , but really you should just rely on the using statement to do this for you as that implicitly calls the Dispose method, for example:

public byte[] HtmlToDoc(string hmtl, string userId)
{
    byte[] data;

    //Add using statement here and wrap it around the rest of the code
    using(var auditor = new ServiceAuditor { User = userId })
    {
        try
        {
            using (var tx = new ServerText())
            {
                tx.Create();
                tx.Load(Server.HtmlDecode(hmtl), StringStreamType.HTMLFormat);
                tx.Save(out data, BinaryStreamType.MSWord);
            }
        }
        catch (Exception e)
        {
            auditor.Errormessage = e.Message + "/n " + e.StackTrace;
            data = new byte[0];
        }
        finally
        {
            auditor.Save();
            //No need to manually dispose here any more
        }
    }

    return data;
}

Thanks @DavidG for your response, definitely there is a point of error in the line mentioned, but what is causing the warning is the initialization of the object:

//Add using statement here and wrap it around the rest of the code
using(var auditor = new ServiceAuditor { User = userId })
{
    try
    { ...

Should be:

using(var auditor = new ServiceAuditor())
{
   auditor.User = userId;
    try
    { ...

I found the reference for this issue here CA2000: Dispose ...

Initializing members of a disposable object should not be done in the constructor of a using statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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