Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd use something like this:</p> <pre><code>var Safe: IObjectSafe; cds1 : TClientDataSet; cds2 : TClientDataSet; cds3 : TClientDataSet; cds4 : TClientDataSet; begin Safe := ObjectSafe; cds1 := Safe.Guard(TClientDataSet.Create(nil)) as TClientDataSet; cds2 := Safe.Guard(TClientDataSet.Create(nil)) as TClientDataSet; cds3 := Safe.Guard(TClientDataSet.Create(nil)) as TClientDataSet; cds4 := Safe.Guard(TClientDataSet.Create(nil)) as TClientDataSet; /////////////////////////////////////////////////////////////////////// /// DO WHAT NEEDS TO BE DONE /////////////////////////////////////////////////////////////////////// // if Safe goes out of scope it will be freed and in turn free all guarded objects end; </code></pre> <p>For the implementation of the interface see <a href="http://dn.codegear.com/article/28217" rel="noreferrer">this</a> article, but you can easily create something similar yourself.</p> <p><strong>EDIT:</strong></p> <p>I just noticed that in the linked article Guard() is a procedure. In my own code I have overloaded Guard() functions that return TObject, above sample code assumes something similar. Of course with generics much better code is now possible...</p> <p><strong>EDIT 2:</strong></p> <p>If you wonder why try ... finally is completely removed in my code: It's impossible to remove the nested blocks without introducing the possibility of memory leaks (when destructors raise exceptions) or access violations. Therefore it's best to use a helper class, and let the reference counting of interfaces take over completely. The helper class can free all objects it guards, even if some of the destructors raise exceptions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload