Recent

Author Topic: [SOLVED] Is BGRABitmap threadsafe?  (Read 1895 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Is BGRABitmap threadsafe?
« on: April 17, 2021, 11:25:30 am »
Is BGRABitmap threadsafe?

I use the following code and have problems

Code: Pascal  [Select][+][-]
  1.   MyDetails := TBGRAWriterWebP.Create;
  2.   MyDetails.QualityPercent := 75;
  3.   MyDetails.Lossless := false;
  4.   MyImage := TBGRABitmap.Create;
  5.   MyImage.LoadFromStream(MyHTTP.Document);
  6.   MyImage.SaveToFile(sOUTDIR + sOUTFILE, MyDetails);
  7.  
  8.   MyHTTP.Document.Position := 0;
  9.   MyThumbail := TBGRABitmap.Create;
  10.   MyThumbail.LoadFromStream(MyHTTP.Document);
  11.   MyImage := MyThumbail.Resample(75, 75);
  12.   MyImage.SaveToFile(sTHUMBNAIL);
  13.  
  14.   MyThumbail.Free;
  15.   MyImage.Free;
  16.   MyDetails.Free;
  17.  

Memory is rapidly leaking.

Any ideas?
« Last Edit: April 18, 2021, 06:29:14 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: Is BGRABitmap threadsafe?
« Reply #1 on: April 17, 2021, 11:56:21 am »
I miss
Code: Pascal  [Select][+][-]
  1. MyHTTP.Free;
in your code.
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Is BGRABitmap threadsafe?
« Reply #2 on: April 17, 2021, 12:01:29 pm »
MyHTTP is freed when the thread is terminated.

Code: Pascal  [Select][+][-]
  1. constructor TMyThread.Create(CreateSuspended : boolean);
  2. begin
  3.   inherited Create(CreateSuspended);
  4.   MyHTTP := THTTPSend.Create;
  5.   MyHTTP.Useragent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0';
  6.   MyHTTP.Sock.OnStatus := @SocketGetCallBack;
  7.   MyBlacklist := TStringList.Create;
  8.   MyBlacklist.Sorted := true;
  9.   MyBlacklist.LoadFromFile('blacklist.txt');
  10.   FreeOnTerminate := True;
  11. end;
  12.  
  13. destructor  TMyThread.Destroy;
  14. begin
  15.   inherited;
  16.   MyHTTP.Free;
  17.   MyBlacklist.Free;
  18. end;
  19.  
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Is BGRABitmap threadsafe?
« Reply #3 on: April 17, 2021, 01:01:00 pm »
MyHTTP is freed when the thread is terminated.
Code: Pascal  [Select][+][-]
  1. destructor  TMyThread.Destroy;
  2. begin
  3.   inherited;
  4.   MyHTTP.Free;
  5.   MyBlacklist.Free;
  6. end;
But in the wrong order, should be:
Code: Pascal  [Select][+][-]
  1. destructor  TMyThread.Destroy;
  2. begin
  3.   MyHTTP.Free;
  4.   MyBlacklist.Free;
  5.   inherited;
  6. end;
« Last Edit: April 17, 2021, 01:03:06 pm by Thaddy »
Specialize a type, not a var.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Is BGRABitmap threadsafe?
« Reply #4 on: April 17, 2021, 01:48:43 pm »
OK, thanks. I'll give it a try. Why, in this case, does it matter?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

sstvmaster

  • Sr. Member
  • ****
  • Posts: 299
Re: Is BGRABitmap threadsafe?
« Reply #5 on: April 17, 2021, 04:13:29 pm »
OK, thanks. I'll give it a try. Why, in this case, does it matter?
Read: https://wiki.freepascal.org/Inherited
greetings Maik

Windows 10,
- Lazarus 2.2.6 (stable) + fpc 3.2.2 (stable)
- Lazarus 2.2.7 (fixes) + fpc 3.3.1 (main/trunk)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Is BGRABitmap threadsafe?
« Reply #6 on: April 17, 2021, 05:30:17 pm »
Thanks.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Is BGRABitmap threadsafe?
« Reply #7 on: April 17, 2021, 05:52:27 pm »
Resample returns a new bitmap. MyImage is created twice, but freed once.
« Last Edit: April 17, 2021, 05:57:22 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

VTwin

  • Hero Member
  • *****
  • Posts: 1215
  • Former Turbo Pascal 3 user
Re: Is BGRABitmap threadsafe?
« Reply #8 on: April 17, 2021, 06:27:22 pm »
Code: Pascal  [Select][+][-]
  1. MyImage.Free;
  2. MyImage := MyThumbail.Resample(75, 75) as TBGRABitmap;
« Last Edit: April 17, 2021, 06:30:16 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.2
macOS 12.1: Lazarus 2.2.6 (64 bit Cocoa M1)
Ubuntu 18.04.3: Lazarus 2.2.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.2.6 (64 bit on VBox)

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Is BGRABitmap threadsafe?
« Reply #9 on: April 17, 2021, 06:33:50 pm »
OK, thanks. I'll give it a try. Why, in this case, does it matter?
It matters because a destructor, just like a constructor can reave havoc when not done in the proper order.
Constructor: inherited first
Destructor: inherited last.
What can happen if the order is wrong is unpredictable. You can introduce bugs you are not aware of. (this is the simple explanation)
In the case of the destructor in the wrong order the class instance may not be there... leading to leaks or crashes.
« Last Edit: April 17, 2021, 06:37:33 pm by Thaddy »
Specialize a type, not a var.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Is BGRABitmap threadsafe?
« Reply #10 on: April 17, 2021, 06:35:30 pm »
Code: Pascal  [Select][+][-]
  1. MyImage.Free;
  2. MyImage := MyThumbail.Resample(75, 75) as TBGRABitmap;

Hi!

Yes  - that is the bug.
Simpler: Replace that line with
Code: Pascal  [Select][+][-]
  1. BGRAreplace ( MyImage, MyThumbail.Resample(75, 75));


Winni
« Last Edit: April 17, 2021, 06:37:41 pm by winni »

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Is BGRABitmap threadsafe?
« Reply #11 on: April 18, 2021, 06:28:21 am »
Thanks all.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

circular

  • Hero Member
  • *****
  • Posts: 4195
    • Personal webpage
Re: [SOLVED] Is BGRABitmap threadsafe?
« Reply #12 on: April 18, 2021, 12:52:14 pm »
To answer the question, BGRABitmap is thread-safe for most things except text rendering.
Conscience is the debugger of the mind

 

TinyPortal © 2005-2018