Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If all you have in the record is an object reference, then you can't get the compiler to help you. You are in sole charge of the lifetime of that object. You cannot overload the assignment operator, and you don't get any notification of scope finalisation.</p> <p>What you can do though is to add a guard interface that will manage the lifetime of the object.</p> <pre><code>TMyRecord = record obj: TMyObject; guard: IInterface; end; </code></pre> <p>You need to make sure that <code>TMyObject</code> manages its lifetime by reference counting. For example by deriving from <code>TInterfacedObject</code>.</p> <p>When you initialise the record you do this:</p> <pre><code>rec.obj := TMyObject.Create; rec.guard := rec.obj; </code></pre> <p>At this point, the <code>guard</code> field of the record will now manage your object's lifetime.</p> <p>In fact, if you want to push this idea further, you can build a dedicated class to guard the lifetime of objects. That then no longer constrains you to implement <code>IInterface</code> on your class. There are plenty of examples on the web that illustrate the technique. For example I offer Jarrod Hollingworth's article titled <a href="http://members.adug.org.au/2011/12/05/smart-pointers/" rel="noreferrer">Smart Pointers</a>, and Barry Kelly's titled <a href="http://blog.barrkel.com/2008/11/reference-counted-pointers-revisited.html" rel="noreferrer">Reference-counted pointers, revisited</a>. There are many more out there. It's an old trick!</p> <p>Note however, that what you have here is a strange hybrid of value type and reference type. On the face of it, records are value types. However, this one acts like a reference type. If you have other fields in the record that are value types then that would be even more confusing. You'll need to be very aware of this issue when you work with such a record.</p> <p>On the face of it, without knowing more about your design, I'd be inclined to advise you not to put object references in records. They fit better inside reference types, i.e. classes.</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. 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