alogrep 0 Posted October 29, 2022 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
Lajos Juhász 293 Posted October 30, 2022 most probably in the part: ..... ///// 2 Share this post Link to post
alogrep 0 Posted October 30, 2022 Thanks. But Madexcept list the line number of TBlobField(FieldByname('image')).SavetoStream(Stream); Share this post Link to post
aehimself 396 Posted October 30, 2022 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
alogrep 0 Posted October 30, 2022 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
Uwe Raabe 2057 Posted October 30, 2022 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. 1 Share this post Link to post
Cristian Peța 103 Posted October 30, 2022 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
Remy Lebeau 1394 Posted October 30, 2022 (edited) 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 October 30, 2022 by Remy Lebeau Share this post Link to post