Stuart Clennett 15 Posted July 4, 2019 (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 July 4, 2019 by Stuart Clennett Share this post Link to post
Andrea Magni 75 Posted July 5, 2019 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 1 Share this post Link to post
Stuart Clennett 15 Posted July 5, 2019 Hi @Andrea Magni Ah brilliant, I wondered what "IsReference" meant 😄 Thanks again Share this post Link to post