Unit refactoring

Fixed massive heap corruption bug in TDataRecord
This commit is contained in:
Michael Schimmel
2025-07-25 11:54:53 +02:00
parent 6b18d95570
commit aa53a88953
13 changed files with 461 additions and 359 deletions
+37 -3
View File
@@ -7,7 +7,7 @@ uses
System.SysUtils,
System.DateUtils,
System.Rtti,
Myc.DataRecord;
Myc.Data.Records;
type
// A record that contains all supported data types for testing
@@ -52,6 +52,10 @@ type
// [IgnoreMemoryLeaks]
procedure TestNestedDataRecord;
[Test]
// [IgnoreMemoryLeaks]
procedure TestInternalRecordAssignment;
[Test]
// [IgnoreMemoryLeaks]
procedure TestNestedSetValueAndGetValue;
@@ -174,7 +178,8 @@ var
begin
// Setup: Create the nested record and convert it to a TDataRecord
nestedSrc.NestedValue := 999;
nestedSrc.NestedString := 'Nested Record Test';
const str = 'Nested Record Test';
nestedSrc.NestedString := str;
nestedDataRecord := TDataRecord.FromRecord<TNestedTestRecord>(nestedSrc);
// Setup: Create the outer record containing the nested TDataRecord
@@ -192,12 +197,41 @@ begin
// Assert values within the nested record
Assert.AreEqual(999, retrievedNestedRecord.GetValue<Integer>('NestedValue'), 'Nested integer value mismatch');
Assert.AreEqual('Nested Record Test', retrievedNestedRecord.GetValue<string>('NestedString'), 'Nested string value mismatch');
Assert.AreEqual(str, retrievedNestedRecord.GetValue<string>('NestedString'), 'Nested string value mismatch');
// Assert value in the outer record
Assert.AreEqual(111, mainDataRecord.GetValue<Integer>('OuterValue'), 'Outer integer value mismatch');
end;
procedure TTestDataRecord.TestInternalRecordAssignment;
var
nestedSrc: TNestedTestRecord;
nestedDataRecord: TDataRecord;
outerSrc: TOuterTestRecord;
mainDataRecord: TDataRecord;
retrievedNestedRecord: TDataRecord;
begin
// Setup: Create the nested record and convert it to a TDataRecord
const str = 'Nested Record Test';
nestedSrc.NestedString := str;
nestedDataRecord := TDataRecord.FromRecord<TNestedTestRecord>(nestedSrc);
// Setup: Create the outer record containing the nested TDataRecord
outerSrc.MyNestedRecord := nestedDataRecord;
// Place a record on the heap. FAILS: This erases the nested record data!!
TDataRecord.FromRecord<TOuterTestRecord>(outerSrc);
// Test: Create the main TDataRecord from the outer record instance
mainDataRecord := TDataRecord.FromRecord<TOuterTestRecord>(outerSrc);
// Assert: Retrieve the nested record and check its contents
retrievedNestedRecord := mainDataRecord.GetValue<TDataRecord>('MyNestedRecord');
// Assert values within the nested record
Assert.AreEqual(str, retrievedNestedRecord.GetValue<string>('NestedString'), 'Nested string value mismatch');
end;
procedure TTestDataRecord.TestNestedSetValueAndGetValue;
var
outerRecord: TDataRecord;