RAD Studio 10.4.1 is now available! Learn more. Looking for discounts? Visit our Special Offers page!
Delphi

Custom Managed Records New in Delphi 10.4 Sydney

RAD Studio and Delphi 10.4 Sydney are available now!

RAD Studio 10.4 available now!

What is a Managed Record in Delphi?

Records in Delphi can have fields of any data type. When a record has plain (non-managed) fields, like numeric or other enumerated values there isn’t much to do for the compiler. Creating and disposing of the record consists of allocating memory or getting rid of the memory location. (Notice that by default Delphi does not zero-initialize records.)

If a record has a field of a type managed by the compiler (like a string or an interface), the compiler needs to inject extra code to manage the initialization or finalization. A string, for example, is reference-counted so when the record goes out of scope the string inside the record needs to have its reference count decreased, which might lead to de-allocating the memory for the string. Therefore, when you are using such a managed record in a section of the code, the compiler automatically adds a try-finally block around that code, and makes sure the data is cleared even in case of an exception. This has been the case for a long time. In other words, managed records have been part of the Delphi language.

Records with Initialize and Finalize Operators 

Now in 10.4, the Delphi record type supports custom initialization and finalization, beyond the default operations the compiler does for managed records. You can declare a record with custom initialization and finalization code regardless of the data type of its fields, and you can write such custom initialization and finalization code. This is achieved by adding specific, new operators to the record type (you can have one without the other if you want).
Below is a simple code snippet:

You need to write the code for the two class methods, of course, for example logging their execution or initializing the record value — here we are also logging a reference to the memory location, to see which record is performing each individual operation:

The huge difference between this construction mechanism and what was previously available for records is the automatic invocation. If you write something like the code below, you can invoke both the initializer and the finalizer, and end up with a try-finally block generated by the compiler for your managed record instance.

With this code, you’ll get a log like:

Another scenario is the use of inline variables, like in:

which gets you the same sequence in the log. 

The Assign Operator

The := assignment flatly copies all of the data of the record fields. While this is a reasonable default, when you have custom data fields and custom initialization you might want to change this behavior. This is why for Custom Managed Records you can also define an assignment operator. The new operator is invoked with the := syntax, but defined as Assign:

The operator definition must follow very precise rules, including having the first parameter as a reference parameter, and the second as a const passed by reference. If you fail to do so, the compiler issues error messages like the following:

There is a sample case invoking the Assign operator:

produces this log (in which we also add a sequence number to the record):

Notice that the sequence of destruction is reversed from the sequence of construction.

Passing Managed Records as Parameters

Managed records can work differently from regular records also when passed as parameters or returned by a function. Here are several routines showing the various scenarios:

Now without going over each log one by one, this is the summary of the information:

  • ParByValue creates a new record and calls the assignment operator (if available) to copy the data, destroying the temporary copy when exiting the procedure
  • ParByConstValue makes no copy, and no call at all
  • ParByRef makes no copy, no call
  • ParByConstRef makes no copy, no call
  • ParReturned creates a new record (via Initialize) and on return, it calls the Assign operator, if the call is like the following, and deletes the temporary record once assigned back like in: my1 := ParReturned;

Exceptions and Managed Records

When an exception is raised, records, in general, are cleared even when no explicit try, finally block is present, unlike objects. This is a fundamental difference and key to the real usefulness of managed records.

Within this procedure, there are two constructor calls and two destructor calls. Again, this is a fundamental difference and a key feature of managed records. See the later section on a simple smart pointer based on managed records.

Arrays of Managed Records

If you define a static array of managed records, there are initialized calling the Initialize operator at the point declaration:

They are all destroyed when they get out of scope. If you define dynamic array of managed records, the initialization code is called with the array is sized (with SetLength):

Conclusion

This is just a relatively short introduction of a great new feature Embarcadero is adding to the Delphi language for the coming 10.4 release. Managed records work for generic records for example and in many other scenarios not covered here. And while this is the top new language feature, there are others coming like the new unified memory management across all platforms. Stay tuned!

If you have Update Subscription, one of the perks is accessing beta builds of upcoming releases. There’s still time to join our beta program for 10.4!

RAD Studio and Delphi 10.4 Sydney are available now!

This is a preview of an upcoming release of RAD Studio. There can always be last-minute bugs or changes. Nothing here is final until the release is officially made available.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Deep Dive Webinar: Boost C++Builder Compile Speed with TwineCompile
About author

Marco is one of the RAD Studio Product Managers, focused on Delphi. He's the best selling author of over 20 books on Delphi.
Related posts
C++DelphiNews

RAD Studio 10.4 Patch 3 is out: VCL Grids, C++ Win64 Debugging, and C++ Android Exception Handling and Resources

C++

10.4 Patch #2: Delphi code completion and IDE fixes

C++

What's New in the GetIt Package Manager - June 2020

C++

RAD Studio 10.4 Now Available, Learn More

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES