Jump to content
Stuart Clennett

Using TFDQuery on DataModule with Resource Result Type TFDDataset

Recommended Posts

Posted (edited)

Hi,

 

Is there anyway I can use a TFDQuery owned by a TDataModule with the resource result type TFDDataset (produces TMediaType.Application_JSON) ? 

 

At present MARS adds the FDQuery to the Activation context, so it tries to free it & it's already freed when the Datamodule is freed (as part of the resource cleanup) in the TMARSActivation.Invoke, e.g.

 

function TAssessmentResource.GetAssessment(const id: string): TFDDataSet;
begin
  // result := FData.qryAssessmentProperties;    <-- results in Invalid Pointer Operation (FData is private DataModule)
  result := FD.CreateQuery(SQL_ASSESSMENT_PROPERTIES, nil, True, 'assessmentProperties');
  TFDQuery(result).Params[0].AsString := id;
end;

The cause is as below:

procedure TMARSActivation.Invoke;
// [..snip..]
begin
  try
  	// [ ... invoke code ... ]
  finally
    // teardown phase
    FTeardownTime := TStopwatch.StartNew;
    if Assigned(FResourceInstance) then
      FResourceInstance.Free;  //<-- datamodule & TFQuery freed here
    FreeContext;               //<-- TFDQuery is in the current context so attempts to free here = Invalid Pointer Operation
    FTeardownTime.Stop;
  end;

 

I'd rather not have to create my own JSON stream, but I'd like to have my DB logic encapsulated in a data module.

 

Thanks


Stuart

 

Edited by Stuart Clennett

Share this post


Link to post

Hi @Stuart Clennett,

you can add the IsReference attribute to instruct MARS not to free the object returned by the method (as it will be destroyed within the datamodule of course, as it is its owner).

 

[GET/POST, IsReference]

function GetAssessment(const id: string): TFDDataSet;

 

Sincerely,

Andrea

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×