I want to fix a design flaw in the TMyValue record that has been used for years, and I desperately want to fix it in the record itself - to avoid changing the general interface - and does not require the code that uses the record to be changed.
In fact, the disadvantage is that TMyValue must be initialized to zero when it is created, otherwise calling the Clear method on it may cause it to fail. This is due to the fact that it contains a field that is a pointer to dynamically allocated memory, if the pointer is not equal to zero, and Clear frees the dynamic memory.
This is a problem if TMyValue is created on the stack because the variables of the stack are not automatically set to zero when they are created.
It seemed to me that I could use a record constructor to nullify a record, but record constructors cannot be without parameters. Presumably this means that you cannot force the write constructor to run automatically when the record is created on the stack.
I suspect the answer to my question is: "This is impossible to do." Please prove that I'm wrong!
source share