Click here to Skip to main content
15,558,034 members
Articles / Programming Languages / C#
Article
Posted 5 Jul 2008

Stats

64.8K views
401 downloads
30 bookmarked

C++/CLI HowTo : Deriving from a C# disposable class

Rate me:
Please Sign up or sign in to vote.
5.00/5 (25 votes)
5 Jul 2008CPOL3 min read
This article walks through the implementation of a C++/CLI class from a disposable C# base.

Introduction

Last week at work, I had to work on a C++/CLI class that derived from a class written in C# which implemented IDisposable. I got it wrong at first (yeah, same guy who wrote a book on the subject a couple of years ago), and my boss and I spent some time going through the generated code in Reflector before fixing it up. I came home that night and quickly put together a simple project so I could see the whole picture from a simple perspective. I thought it would benefit others to put together an article reflecting (no pun intended) the issue.

The C# base class

Here's a simple C# class that implements IDisposable. It's assumed that the class handles both managed and native resources.

C#
namespace DisposeDemo
{
    [SuppressMessage("Microsoft.Naming", 
        "CA1709:IdentifiersShouldBeCasedCorrectly", 
        MessageId = "Cs", 
        Justification = "Personal preference")]
    public class CsDisposableBase : IDisposable
    {
        #region IDisposable Members

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~CsDisposableBase()
        {
            Dispose(false);
        }

        /// <span class="code-SummaryComment"><summary></span>
        /// Derived classes need to override this appropriately
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="disposing">Indicates whether this is a dispose call</span>
        /// or a call invoked from the finalzier<span class="code-SummaryComment"></param></span>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                DisposeManagedResources();
            }

            FreeNativeResources();
        }

        #endregion

        private void DisposeManagedResources()
        {
            this.WriteLine("Disposing managed resources in base class");
        }

        private void FreeNativeResources()
        {
            this.WriteLine("Freeing native resources in base class");
        }

        public CsDisposableBase(String name)
        {
            this.Name = name;
        }

        public String Name { get; private set; }

        protected void WriteLine(String text)
        {
            Console.WriteLine("{0} : {1}", this.Name, text);
        }
    }
}

For any C# developer inheriting from this class, it's a no-brainer to override and implement void Dispose(bool) as the class requirements dictate. But things are not so obvious from C++/CLI. C++/CLI does not permit any method named "Dispose" in your class. While you've all probably implemented IDisposable classes in C++/CLI before, you may not have yet derived from a C# IDisposable base before. Anyway, the trick is to avoid over-thinking and complicating the issue - all you need to do is to handle it just as you'd do normally. Put the managed-disposal in the destructor and the native-disposal in the finalizer.

The C++/CLI derived class

Here's the derived class implementation in C++/CLI:

MC++
namespace CppDisposable
{
  public ref class DisposableDerived : CsDisposableBase
  {
  private:
    void DisposeManagedResourcesDerived()
    {
      WriteLine("Disposing managed resources in derived class");
    }

    void FreeNativeResourcesDerived()
    {
      WriteLine("Freeing native resources in derived class");
    }

  public:
    ~DisposableDerived()
    {
      DisposeManagedResourcesDerived();
      this->!DisposableDerived();
    }

    !DisposableDerived()
    {
      FreeNativeResourcesDerived();
    }

    DisposableDerived(String^ name) : CsDisposableBase(name)
    {
    }

  };
}

At first glance, the code seems wrong. How do we ensure that the base class disposal code (and the finalizer) are called as and when needed? Let's write some test code to see what happens.

MC++
int main(array<System::String ^> ^args)
{
  CppDisposable::DisposableDerived object1(
    "Object that gets disposed");

  CppDisposable::DisposableDerived^ object2 =
    gcnew CppDisposable::DisposableDerived("Object that gets GCd");
  return 0;
}

Running that gives the following output :-

Image 1

Wow. Everything worked fine. How did that happen? The answer lies in VC++ compiler magic. A look at the generated code using Reflector shows us that the compiler has generated the code that we may otherwise have had to write ourselves.

What Reflector shows

Here's the generated code (as viewed in Reflector):

C#
protected override void Dispose([MarshalAs(UnmanagedType.U1)] bool flag1)
{
    if (flag1)
    {
        try
        {
            this.~DisposableDerived();
        }
        finally
        {
            base.Dispose(true);
        }
    }
    else
    {
        try
        {
            this.!DisposableDerived();
        }
        finally
        {
            base.Dispose(false);
        }
    }
}

So, during a Dispose call, here's what will happen:

  1. The C# base class Dispose will call Dispose(true) and suppress GC.
  2. The C++/CLI implementation will call ~DisposableDerived (which frees the managed resources in the derived class and then calls !DisposableDerived which frees the native resources in the derived class).
  3. Now, base.Dispose(true) is called (in the finally-block) which frees the managed and native resources in the base class.

And this is what happens during a finalization:

  1. The C# base class finalizer calls Dispose(false).
  2. The C++/CLI implementation calls !DisposableDerived (which frees the native resources in the derived class).
  3. And base.Dispose(false) is called (in the finally-block) which frees the native resources in the base class.

Essentially, this code is exactly what we'd have needed to write - the compiler generates it for us.

Conclusion

I know this is a very simple article, but sometimes it's the simple things that we stumble over. I wish I had covered this topic in my book (where I talk about deterministic destruction). As always, please feel free to submit feedback (critical or otherwise).

History

  • July 5, 2008 - Article published.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a Principal Software Architect based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website : www.ganymedesoftwaresolutions.com

Comments and Discussions

 
QuestionIs this information still accurate? Pin
Member 1140839628-Jan-15 1:07
Member 1140839628-Jan-15 1:07 
AnswerRe: Is this information still accurate? Pin
Nish Nishant20-Apr-15 16:18
sitebuilderNish Nishant20-Apr-15 16:18 
GeneralMy 5! Pin
RakeshMeena13-Jun-11 23:14
RakeshMeena13-Jun-11 23:14 
Generalto ~ or to !.. Pin
gooja19-Sep-08 4:29
gooja19-Sep-08 4:29 
GeneralRe: to ~ or to !.. Pin
gooja19-Sep-08 5:08
gooja19-Sep-08 5:08 
GeneralHelp me have class stand Pin
medop8225-Aug-08 0:29
medop8225-Aug-08 0:29 
I had to buid the programs but when i run it which had to occupy butffer. So that i need to help to resolve this problem.
Evething idea please send fllowing Address mail: medop82@yahoo.com
Thanks you very much!
the class of me

public class CatalogueArea
{
#region Define
private System.Data.SqlClient.SqlConnection con = Medop.Define.Global.Static.ClassStatic.con;
private string[] _ArrayReturn = new string[] { Medop.Define.Global.Static.ClassStatic.DefaultEmpty, Medop.Define.Global.Static.ClassStatic.DefaultEmpty, Medop.Define.Global.Static.ClassStatic.DefaultEmpty };
private System.Data.DataSet _DsNull = null;
private string _CodeReturn;
#endregion Define

#region Contruct
public CatalogueArea()
{
}
#endregion Contruct

#region Public
public int VoidCheckExit(string input, string NameFile, string NameTable)
{
try
{
string sql;
if (con.State != ConnectionState.Open) con.Open();
sql = "SELECT * from " + NameTable + " where 1=1 and " + NameFile + "=" + "'" + input + "' ";
SqlCommand cm = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet mDataset = new DataSet();
cm = new SqlCommand(sql, con);
cm.ExecuteNonQuery();
da = new SqlDataAdapter(sql, con);
da.Fill(mDataset, Define.Global.Static.ClassStatic.NameTable);
da.Dispose();this.con.Close();

if (mDataset.Tables[Medop.Define.Global.Static.ClassStatic.NameTable].Rows.Count != 0)
return 0;
else
return 1;
}
catch
{
return 10;
}
}
public DataSet VoidGetNoneDataset()
{
try
{
string sql;
if (con.State != ConnectionState.Open) con.Open();
sql = "SELECT 0 AS STT, CODE_AREA, NAME_AREA, NOTE FROM dbo.AREA WHERE (1 <>1)";
SqlCommand cm = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet mDataset = new DataSet();
cm = new SqlCommand(sql, con);
cm.ExecuteNonQuery();
da = new SqlDataAdapter(sql, con);
da.Fill(mDataset, Define.Global.Static.ClassStatic.NameTable);
da.Dispose();this.con.Close();

return mDataset;
}
catch
{
return _DsNull;
}
}
public int VoidInsert(string CODE_AREA, string NAME_AREA, string NOTE)
{
try
{
if (VoidGetDataset(CODE_AREA, true).Tables[Medop.Define.Global.Static.ClassStatic.NameTable].Rows.Count != 0)
{
return 0;
}
string sql = Medop.Define.Global.Static.ClassStatic.DefaultEmpty;
sql = " set dateformat dmy; INSERT INTO dbo.AREA (";
sql += "CODE_AREA,";
sql += "NAME_AREA,";
sql += "NOTE";
sql += ") values (";
sql += "N'" + CODE_AREA + "'";
sql += ",N'" + NAME_AREA + "'";
sql += ",N'" + NOTE + "'";
sql += ")";
if (con.State != ConnectionState.Open) con.Open();
SqlCommand vCommand = new SqlCommand(sql, con);
vCommand.ExecuteNonQuery();
vCommand.Dispose();

_CodeReturn = CODE_AREA;
return 1;
}
catch
{
return 10;
}
}
public int VoidUpdate(string CODE_AREA, string NAME_AREA, string NOTE)
{
try
{
SqlCommand vCommand = new SqlCommand();
string sql = Medop.Define.Global.Static.ClassStatic.DefaultEmpty;
sql = "set dateformat dmy; update dbo.AREA set ";
sql += "NAME_AREA=N'" + NAME_AREA.Trim() + "'";
sql += " ,NOTE=N'" + NOTE.Trim() + "'";
sql += " WHERE CODE_AREA=N'" + CODE_AREA.Trim() + "'";
if (con.State != ConnectionState.Open) con.Open();
vCommand = new SqlCommand(sql, con);
vCommand.ExecuteNonQuery();
vCommand.Dispose();

_CodeReturn = CODE_AREA;
return 1;
}
catch
{
return 10;
}
}
public int VoidDelete(string CODE_AREA)
{
try
{
string sql = Medop.Define.Global.Static.ClassStatic.DefaultEmpty;
sql = "delete from dbo.AREA where CODE_AREA=N'" + CODE_AREA.Trim() + "'";
if (con.State != ConnectionState.Open) con.Open();
SqlCommand vCommand = con.CreateCommand();
vCommand.CommandText = sql;
vCommand.ExecuteNonQuery();
vCommand.Dispose();

return 1;
}
catch
{
return 10;
}
}
#endregion Public

#region Private
public DataSet VoidGetDataset(string _Input, bool CodeorAll)
{
try
{
string sql;
if (con.State != ConnectionState.Open) con.Open();
if (CodeorAll)
{
if (_Input.Trim().Equals(Medop.Define.Global.Static.ClassStatic.DefaultEmpty))
{
sql = "SELECT 0 AS STT, CODE_AREA, NAME_AREA, NOTE FROM dbo.AREA where 1=1 order by CODE_AREA,NAME_AREA,NOTE";
}
else
{
sql = "SELECT 0 AS STT, CODE_AREA, NAME_AREA, NOTE FROM dbo.AREA where 1=1 and CODE_AREA=N'" + Medop.Dispose.Conveniently.Conveniently.Replate(_Input.Trim()) + "' order by CODE_AREA,NAME_AREA,NOTE";
}

}
else
{
if (_Input.Trim().Equals(Medop.Define.Global.Static.ClassStatic.DefaultEmpty))
{
sql = "SELECT 0 AS STT, CODE_AREA, NAME_AREA, NOTE FROM dbo.AREA where 1=1 order by CODE_AREA,NAME_AREA,NOTE";
}
else
{
sql = "SELECT 0 AS STT, CODE_AREA, NAME_AREA, NOTE FROM dbo.AREA where 1=1 and CODE_AREA like N'%" + Medop.Dispose.Conveniently.Conveniently.Replate(_Input.Trim()) + "%' or NAME_AREA like N'%" + Medop.Dispose.Conveniently.Conveniently.Replate(_Input.Trim()) + "%' order by CODE_AREA,NAME_AREA,NOTE";
}
}
SqlCommand cm = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataSet mDataset = new DataSet();
cm = new SqlCommand(sql, con);
cm.ExecuteNonQuery();
da = new SqlDataAdapter(sql, con);
da.Fill(mDataset, Define.Global.Static.ClassStatic.NameTable);
da.Dispose();this.con.Close();
if (mDataset.Tables[Define.Global.Static.ClassStatic.NameTable].Rows.Count != 0)
{
_ArrayReturn[0] = mDataset.Tables[Define.Global.Static.ClassStatic.NameTable].Rows[0][Medop.Define.FieldInDatabase.StaticCatalogue.CODE_AREA].ToString().Trim();
_ArrayReturn[1] = mDataset.Tables[Define.Global.Static.ClassStatic.NameTable].Rows[0][Medop.Define.FieldInDatabase.StaticCatalogue.NAME_AREA].ToString().Trim();
_ArrayReturn[2] = mDataset.Tables[Define.Global.Static.ClassStatic.NameTable].Rows[0][Medop.Define.FieldInDatabase.StaticCatalogue.NOTE].ToString().Trim();
}

return Medop.Dispose.Conveniently.Conveniently.MakeStandardDataSet(mDataset);
}
catch
{
return null;
}

}

#endregion Private

#region Property

public string[] ArrayReturn
{
get { return _ArrayReturn; }
}
public string CodeReturn
{
get { return _CodeReturn; }
}
#endregion
}

đ

GeneralQuestion Pin
Przemyslaw Celej13-Jul-08 11:36
Przemyslaw Celej13-Jul-08 11:36 
GeneralRe: Question Pin
Nish Nishant13-Jul-08 17:42
sitebuilderNish Nishant13-Jul-08 17:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.