.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dispose: when and when not?

1 REPLY 1
SOLVED
Reply
Message 1 of 2
seabrahenrique
194 Views, 1 Reply

Dispose: when and when not?

Hello, guys!

 

I saw two articles about dispose objects here, and here. And then, i understand by resuming that i just have to manual dispose (by using or .dispose) newly objects derived from DBObject.

 

Based on that, my first doubt is: Could I not dispose the Result Buffer in the code below?

 

        public static void SetXRecord(DBObject dbObj, string key, string value)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBObject obj = tr.GetObject(dbObj.ObjectId, OpenMode.ForWrite);

                if (obj == null) return;

                if (obj.ExtensionDictionary.IsNull) obj.CreateExtensionDictionary();

                DBDictionary dic = tr.GetObject(obj.ExtensionDictionary, OpenMode.ForWrite) as DBDictionary;

                Xrecord xrec;

                if (dic.Contains(key))
                {
                    xrec = tr.GetObject(dic.GetAt(key), OpenMode.ForWrite) as Xrecord;
                }
                else
                {
                    xrec = new Xrecord();
                    //if (!dic.IsWriteEnabled) tr.GetObject(dic.ObjectId, OpenMode.ForWrite);
                    dic.SetAt(key, xrec);
                    tr.AddNewlyCreatedDBObject(xrec, true);
                }

// ######## ~ HERE I DISPOSE BUT I CANT HAVE TO ~ ##########
                using (ResultBuffer rb = new ResultBuffer(new TypedValue((int)DxfCode.Text, value)))
                {
                    xrec.Data = rb;
                }

                tr.Commit();
            }
        }

 

I know is there a post quite similar here, but cleaely the theme divide opinions, and i can't conclude what is the best pratice.

 

So, two doubts about that subject for this post:

 

1. In that sample, do i have to Dispose Result Buffer? (code works perfect do it or not)

 

2.  In general, can i trust in GC and just use Dispose in cases indicatade in the posts that i put above? Or is a best pratice do to always i can, in all objects that implement IDisposable?

 

Thanks in advance.

1 REPLY 1
Message 2 of 2

The ResultBuffer doesn't have to be deterministically-disposed, because its destructor is thread-safe. You can dispose it if you want, but if you don't it will be disposed by the GC. Not disposing a ResultBuffer would only become an issue if the contents is huge, or there are many of them, which could result in some performance degradation.

 

The reason why some objects (e.g., DBObjects) must be disposed is because if you leave it to the GC, the object's native destructor will be called on a background thread, where it can't access the API, and that often leads to a failure.

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report