Jump to content
alogrep

Memory leak on TBlobField(FieldByname(field_name)).SavetoStream(Stream);

Recommended Posts

Hi.

 

Madexcept shows a leak on this part of my code:

 TBlobField(FieldByname('image')).SavetoStream(Stream);

 var
      Stream: TMemoryStream;
    ....        

         Stream:=TMemoryStream.create;
        TBlobField(FieldByname('image')).SavetoStream(Stream);
        if Stream.Size > 0 then  begin
          FindGraphicClass(Stream.Memory^, Stream.Size, GraphicClass);
          .....
          /////     
        end;
        Stream.free;
    end;

Anybody knows that is wrong with my code?

Share this post


Link to post

Thanks. But Madexcept list the line number of 

TBlobField(FieldByname('image')).SavetoStream(Stream);

Share this post


Link to post

All programs only show you the place the leaked object was created. It has no idea where you want to free it up.

 

 What is the leaked object?

Share this post


Link to post

Ok this is (a bit modified ) code

with dm2,memfloorobjects do begin
      setit(memfloorobjects,'no');
      edit;
      BlobStream:=TnxBlobStream(memfloorobjects.CreateBlobStream(fieldbyname('image'),bmRead));
      try
        if TnxBlobStream(BlobStream).Size > 0 then  begin
          FindGraphicClass(BlobStream, BlobStream.Size, GraphicClass);
          TnxBlobStream(BlobStream).Position := 0;
          b:=Tbitmap.Create;
          b.LoadFromStream(TnxBlobStream(BlobStream)); //line with leak???
          try
            try
              Image.Picture.assign(b);
            except
                 on E:SysUtils.Exception do begin
                   if debughook <> 0 then
                      showmessage(inttostr(picno)+'  '+E.message);
                 end;
            end;
          finally
            b.free;
          end;
        end;
     finally
        TnxBlobStream(BlobStream).free;
        cancel;
     end;
      
end;

Madexcept says that object created there on thah line with ??? leaked. Why? I did free it.

Share this post


Link to post

How do you guarantee that memfloorobjects.CreateBlobStream actually returns a TnxBlobStream?

What is a TnxBlobStream?

How is BlobStream declared?

What is memfloorobjects?

Why do you list memfloorobjects in the with and still reference it in the code so no one can see at a glance where it is used?

Why do you use with at all as it only obfuscates the code here?

What is GraphicClass and where it is used?

Why is the try not directly after TBitmap.Create?

Why do you cast to TnxBlobStream when Free is already declared in TObject?

1 hour ago, alogrep said:

Madexcept says that object created there on thah line with ??? leaked. Why? I did free it.

What object is created in that line? Neither b nor BlobStream are created in that line and those are the only objects you free.

  • Like 1

Share this post


Link to post
5 hours ago, alogrep said:

 


          b:=Tbitmap.Create;
          b.LoadFromStream(TnxBlobStream(BlobStream)); //line with leak???

 

TBitmap.LoadFromStream() will call TStream.ReadBuffer().

Maybe TnxBlobStream.ReadBuffer() is leaking. Have you checked it?

Share this post


Link to post
13 hours ago, alogrep said:

 


b:=Tbitmap.Create;
b.LoadFromStream(TnxBlobStream(BlobStream));
try
...

LoadFromStream() can potentially raise an exception, which will bypass your Free'ing of the TBitmap. Move LoadFromStream() inside the try..except block.

 

Edited by Remy Lebeau

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

×