Invalid pointer operation

I am infrequently getting an "Invalid pointer operation" error on a GET. When this does happen subsequent GETs also may get the error and eventually XData service fails to respond. I understand that this invalid pointer error error may indicate that an attempt has been made to free an object that has already been freed. After creating the Result object in the service function I am using the statement TXDataOperationContext.Current.Handler.ManagedObjects.Add(Result). Result is not specifically freed elsewhere.

I am still relatively new to XData so would appreciate any suggestions on where to look for the cause of the error.

some code snippets

In production the Invalid pointer operation occurs infrequently. I can emulate this with a test program firing many requests over a minute or two.


  [Entity, Automapping]
  TServerVersion = class
  private
    FID: Integer;
    FMajor: Integer;
    FMinor: Integer;
    FRelease: Integer;
  public
    constructor Create(AID, AMajor, AMinor, ARelease: Integer);
    property ID: Integer read FID;
    property Major: Integer read FMajor;
    property Minor: Integer read FMinor;
    property Release: Integer read FRelease;
  end;
  
  
    [Route('GetServerVersion')]
    [HttpGet] function GetServerVersion: TServerVersion;


function TChildInquiryService.GetServerVersion: TServerVersion;
var
  AMajor, AMinor, ARelease, ABuild: Word;
  AID: Integer;
begin

  uHandy.GetProgramVersion(ParamStr(0), AMajor, AMinor, ARelease, ABuild);

  AID := 1;

  Result := TServerVersion.Create(AID, AMajor, AMinor, ARelease);

  TXDataOperationContext.Current.Handler.ManagedObjects.Add(Result);

end;

Are you sure this is the only code you have? I don't see any problem with it.
Note that you don't need to call ManagedObjects.Add(Result), XData already automatically destroys the object being returned by the function.

What is common to happen is when the returned object also has an associated object. By default, XData will also tries to destroy such object. For example, suppose TServerVersion has a property Obj: TAnotherObject. Then XData will destroy Obj too. If TServerVersion calls Obj.Free in its destruction, an invalid pointer might happen.

The easiest way to solve this problem is to add a [JsonManaged] attribute to the TServerVersion class, telling XData to never destroy any associated object of the class.

Wagner

Thanks for the information about the Result object.

I am pleased to say that I have now spotted my problem. I am using the CodeSite debugging tool from Raize. It was creating an object which appears to be conflicting with the XData routines. Dropping Codesite completely seems to be working perfectly. I should have just stuck to your excellent TMS logging system!

Thanks for your help.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.