#1 2018-09-06 05:14:42

BugsDigger
Member
Registered: 2018-09-06
Posts: 2

Some methods of SynGdiPlus are not thread-safe

Hi, Synopse and Arnaud!
Many thanks for your highly useful GDI unit!

During my development I've found that using SynGdiPlus (I use only LoadFromFile/ToBitmap/SaveToFile to work with tif files) in background thread sometimes leads to "black squares" instead of normal image in bitmaps.
Usually it is symptom of multithreading problems with Canvas.

SynGdiPlus uses Canvas.Draw without Lock/Unlock.
I've added the following changes:

begin
  if not fHasContent then
    ...
  else begin
    ...
    result.Canvas.Lock;
    try
       result.Canvas.Draw(0,0,self);
    finally
       result.Canvas.Unlock;
    end;
  end;
end;

procedure SaveAs(...)
...
        Bmp := TBitmap.Create;
        try
          ...
          Bmp.Canvas.Lock;
          try
             Pic.Draw(Bmp.Canvas,R);
          finally
             Bmp.Canvas.Unlock;
          end;
          SynGdiPlus.SaveAs(Bmp,Stream,Format,CompressionQuality,0,BitmapSetResolution);
        finally
          Bmp.Free;
        end;

After that I see no problems in intensive tests.

(Also for all who want to use SynGdiPlus in background thread: don't forget to use CoInitializeEx/CoUninitialize.)

Offline

#2 2018-09-06 13:55:48

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,649
Website

Re: Some methods of SynGdiPlus are not thread-safe

Please check https://synopse.info/fossil/info/93f8369d36

Thanks a lot for your input!

Offline

Board footer

Powered by FluxBB