Jump to content

Recommended Posts

This code gives an error, "Invalid Pointer Operation".

 

It's the aBlob.Free that triggers the error.

 

What's wrong in my code? I should Free I guess?

 

var
  aBlob : TBlobField;
  aStream : TStream;   

try
      ablob:=aQuery.fieldbyname('Memo') as tblobfield;
      astream:=Tmemorystream.Create;
      astream := aquery.CreateBlobStream(ablob, bmRead);
      amemo.Lines.LoadFromStream(astream);
    finally
      ablob.Free;
      astream.Free;
    end;

Share this post


Link to post

Why are you freeing the TBlobField at all?  You don't own it, the Query component does, so you should not be freeing it at all.

 

Also, you are leaking the TMemoryStream that you create (which you should not be creating in the first place).

 

The only thing you should be freeing manually is the TStream that CreateBlobStream() returns, eg:

var
  aBlob : TField;
  aStream : TStream;
begin
  aBlob := aQuery.FieldByName('Memo');
  aStream := aQuery.CreateBlobStream(aBlob, bmRead);
  try
    aMemo.Lines.LoadFromStream(aStream);
  finally
    aStream.Free;
  end;
end;

 

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post

It's important to learn how to try finally is used. Like this 

 

resource := acquireResource;

try

  resource.doSomething;

finally

  resource.release;

end;

 

I've not use standard names here, but gone for a more conceptual presentation. 

 

The key is the order of things. 

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

×