Mark-sai Respected Contributor.
Respected Contributor.
600 views

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?

0 Likes
5 Replies
Matt Bayliss Super Contributor.
Super Contributor.

Re: disposing object for .net SDK

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: 

 

 

Highlighted
Micro Focus Expert
Micro Focus Expert

Re: disposing object for .net SDK

One minor tweak, Database is by far the most common Disposable, but there are two others.  As documented in the new SDK documentation.


Blog | Samples | HPE CM 9.3 SDK Docs
**Any opinions expressed in this forum are my own personal opinion and should not be interpreted as an official statement on behalf of MicroFocus**
Mark-sai Respected Contributor.
Respected Contributor.

Re: disposing object for .net SDK

I can't seem to be able to use record.Dispose() in RM8.3. Looks like it is expecting parameter in it.

0 Likes
Mark-sai Respected Contributor.
Respected Contributor.

Re: disposing object for .net SDK

If I use something like this, it works.

rmRecord.Dispose(DisposalType.None, true);

0 Likes
Micro Focus Expert
Micro Focus Expert

Re: disposing object for .net SDK

That would be this method:

 

public void Dispose(DisposalType methodOfDisposal, bool continueOnError);

 

Which is the method to dispose a Record in RM, not to dispose the memory for an object.


Blog | Samples | HPE CM 9.3 SDK Docs
**Any opinions expressed in this forum are my own personal opinion and should not be interpreted as an official statement on behalf of MicroFocus**
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.