disposing object for .net SDK

Hi 

I am using .net SDK in my .net application and wondering if I need to dispose the object explicitly in order to clean up?

e.g

if i have Record record=new Record(objDB,"Document")

 

Then in dispose, do I have to dispose record object explicitly?

Parents
  • For the .Net SDK in RM 8.1 and earlier, and TRIM 7, yes, you definitely have to dispose of every object that implements IDisposable.

    In RM 8.2 (and I assume going forward), you only have to dispose of the Database object, which is so much better, but breaks all the legacy code targetting earlier versions.

    The easiest way of disposing objects is by using using:

     

    using (var record = new Record(objDB, "Document") {
       // code that uses record
    }

    And the record will be disposed automatically.

     

    Otherwise, something explicit, like:

     

    Record record = null;
    try
    {
        record = new Record(objDB, "Document");
        //record code stuff
    }
    catch(Exception)
    {
        // error handling
    }
    finally
    {
        if(record != null)
        {
            record.Dispose();
        }
    }

     

     

    To make code that supports all 8.2 AND earlier versions you could have an object extension such as:

    public static void DisposeIfDisposable(this object obj)
    {
        if((obj != null) && (obj is IDisposable))
        {
            ((IDisposable)obj).Dispose();
        }
    }

    Then all your code could be like:

    Record record = null;
    try
    {
        record = new Record(objDB, "Document");
        //record code stuff
    }
    catch(Exception)
    {
        // error handling
    }
    finally
    {
        record.DisposeIfDisposable();
    }

    Hope that helps,

    Matt.

     

    Edit:

    Further reading: 

     

     

Reply
  • For the .Net SDK in RM 8.1 and earlier, and TRIM 7, yes, you definitely have to dispose of every object that implements IDisposable.

    In RM 8.2 (and I assume going forward), you only have to dispose of the Database object, which is so much better, but breaks all the legacy code targetting earlier versions.

    The easiest way of disposing objects is by using using:

     

    using (var record = new Record(objDB, "Document") {
       // code that uses record
    }

    And the record will be disposed automatically.

     

    Otherwise, something explicit, like:

     

    Record record = null;
    try
    {
        record = new Record(objDB, "Document");
        //record code stuff
    }
    catch(Exception)
    {
        // error handling
    }
    finally
    {
        if(record != null)
        {
            record.Dispose();
        }
    }

     

     

    To make code that supports all 8.2 AND earlier versions you could have an object extension such as:

    public static void DisposeIfDisposable(this object obj)
    {
        if((obj != null) && (obj is IDisposable))
        {
            ((IDisposable)obj).Dispose();
        }
    }

    Then all your code could be like:

    Record record = null;
    try
    {
        record = new Record(objDB, "Document");
        //record code stuff
    }
    catch(Exception)
    {
        // error handling
    }
    finally
    {
        record.DisposeIfDisposable();
    }

    Hope that helps,

    Matt.

     

    Edit:

    Further reading: 

     

     

Children