We cannot reproduce the issue with just the above code. But note that when you call ReplaceDefaultInstance, it already destroys the existing mapping explorer there. So if FMappingExplorer is also the default one, you should not destroy it.
But we cannot guess what's going on since some code is missing. Maybe you can provide a separate, standalone project that reproduces the issue.
The access violation does not exactly occur in the code above. It occurs when loading an entity from the database, during a GET XData request. I'm using "Table" attribute in my entity, and TDynamicColumn column in my dynamic fields.
Create and configure your dynamic properties before you use it. So, you must do it before you start the server:
procedure TMainForm.btStartClick(ASender: TObject);
begin
TCustomFields.CreateDynamicProps;
ServerContainer.SparkleHttpSysDispatcher.Start;
UpdateGUI;
end;
Also, you don't need to destroy the new mapping explorer instance you created if you set it as the default one. In this case, Aurelius will destroy it automatically:
class procedure TCustomFields.CreateDynamicProps;
var
MappingSetup: TMappingSetup;
DynamicProps: TList<TDynamicProperty>;
begin
//Exit;
MappingSetup := TMappingSetup.Create;
try
DynamicProps := MappingSetup.DynamicProps[TPerson];
DynamicProps.Add(
TDynamicProperty.Create('CustomFields', 'CustomField1', TypeInfo(string),
TDynamicColumn.Create('CustomField1', [], 20)));
TMappingExplorer.ReplaceDefaultInstance(TMappingExplorer.Create(MappingSetup));
finally
MappingSetup.Free;
end;
end;