<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Dispose: when and when not? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/dispose-when-and-when-not/m-p/12934075#M3109</link>
    <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 01 Aug 2024 18:34:19 GMT</pubDate>
    <dc:creator>ActivistInvestor</dc:creator>
    <dc:date>2024-08-01T18:34:19Z</dc:date>
    <item>
      <title>Dispose: when and when not?</title>
      <link>https://forums.autodesk.com/t5/net-forum/dispose-when-and-when-not/m-p/12933979#M3108</link>
      <description>&lt;P&gt;Hello, guys!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I saw two articles about dispose objects &lt;A href="https://help.autodesk.com/view/OARX/2022/ENU/?guid=GUID-9DFB5767-F8D6-4A88-87D6-9676C0189369" target="_blank" rel="noopener"&gt;here&lt;/A&gt;, and &lt;A href="https://www.keanw.com/2008/06/cleaning-up-aft.html" target="_blank" rel="noopener"&gt;here&lt;/A&gt;. And then, i understand by resuming that &lt;STRONG&gt;i just have to manual dispose (by using or .dispose) newly objects derived from DBObject.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Based on that, my first doubt is: Could I not dispose the Result Buffer in the code below?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;        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();
            }
        }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know is there a post quite similar&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/net/shall-i-dispose-result-buffer-in-this-sample-function/td-p/3366643" target="_blank" rel="noopener"&gt;here,&lt;/A&gt;&amp;nbsp;but cleaely the theme divide opinions, and i can't conclude what is the best pratice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;So, two doubts about that subject for this post:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. In that sample, do i have to Dispose Result Buffer? (code works perfect do it or not)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2.&amp;nbsp; 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?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2024 17:43:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/dispose-when-and-when-not/m-p/12933979#M3108</guid>
      <dc:creator>seabrahenrique</dc:creator>
      <dc:date>2024-08-01T17:43:31Z</dc:date>
    </item>
    <item>
      <title>Re: Dispose: when and when not?</title>
      <link>https://forums.autodesk.com/t5/net-forum/dispose-when-and-when-not/m-p/12934075#M3109</link>
      <description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Aug 2024 18:34:19 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/dispose-when-and-when-not/m-p/12934075#M3109</guid>
      <dc:creator>ActivistInvestor</dc:creator>
      <dc:date>2024-08-01T18:34:19Z</dc:date>
    </item>
  </channel>
</rss>

