Dynamic properties raise an access violation

Hi,

I'm looking for a sample showing dynamics properties in action.

The following lines in my project raise an access violation :

class procedure TCustomFields.CreateEntitesDynamicProps;
var
  MappingSetup: TMappingSetup;
  DynamicProps: TList<TDynamicProperty>;
begin
  MappingSetup := TMappingSetup.Create;
  try
    // TArticle
    DynamicProps := MappingSetup.DynamicProps[TArticle];
    DynamicProps.Add(
      TDynamicProperty.Create('CustomFields', 'CustomField1', TypeInfo(string),
        TDynamicColumn.Create('XXX_TEXTE1', [], 20)));

    // TTiers
    DynamicProps := MappingSetup.DynamicProps[TTiers];
    DynamicProps.Add(
      TDynamicProperty.Create('CustomFields', 'CustomField2', TypeInfo(string),
        TDynamicColumn.Create('XXX_TEXTE1', [], 20)));

    // TMappingExplorer.ReplaceDefaultInstance
    FreeAndNil(FMappingExplorer);
    FMappingExplorer := TMappingExplorer.Create(MappingSetup);
    TMappingExplorer.ReplaceDefaultInstance(FMappingExplorer);
  finally
    MappingSetup.Free;
  end;
end;

Emmanuel

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.

Thank you. I'll try to provide a standalone project that reproduces the issue as soon as possible.

Is there a sample project that shows dynamics properties in action ? There is no one neither in the Demos folder nor in the documentation.

Did you try with a MSSQL database ? because I'm unable to reproduce with a SQLite database.

There should be no difference regardless of the database. There is no database connection involved in that code.

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.

What could raise the access violation ?

The call stack :

Is there a sample project that shows dynamics properties in action ?

There is no one neither in the Demos folder nor in the documentation.

Here is a sample app which reproduces the access violation.

You have inside a .sql file to help you creating the SQL Server database.

Could you tell me what's wrong ?

DynamicFieldsAccessViolation.zip (93 KB)

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;