Using Delphi 10.2.3:
I am writing code that repeat-decodes the same set of PNG images in multiple threads over and over again.
Once the thread is executed, it uses an FMX TBitmap component's "LoadFromStream" method to decode a PNG file loaded into a TMemoryStream (within the thread).
Running under Windows, no issues.
Running under Android I get multiple exceptions and it appears to trigger randomly on just some of the threads:
1. Exception "Can not activate current context"
2. EReadError "stream error"
If I capture the exception and save the stream to a file, the PNG is valid.
If I synchronize the decoding "Bitmap.LoadFromStream(MemoryStream)" function everything works.
If the answer is that PNG decoding is not thread safe using the native library, is there an alternative solution that does support multithreaded PNG decoding under Android?
Sample code:
procedure TImageDecodeThread.Execute;
var
memStream : TMemoryStream;
dlBitmap : TBitmap;
Begin
memStream := TMemoryStream.Create;
Try
memStream.LoadFromFile('image'+ThreadName+'.png');
Except
on E : Exception do
Begin
DebugEntry('memstream:'+E.ClassName+', "'+E.Message+'", Size='+IntToStr(memStream.Size));
End;
End;
memStream.Position := 0;
dlBitmap := TBitmap.Create;
Try
dlBitmap.LoadFromStream(memStream);
Except
on E : Exception do
Begin
DebugEntry('decode'+E.ClassName+', "'+E.Message+'", Size='+IntToStr(memStream.Size));
memStream.Position := 0;
memStream.SaveToFile(ThreadName+'exception'.png');
End
End;
memStream.Free;
dlBitmap.Free;
End;
Update
I tried to wrap the TBitmap's LoadFromStream method inside a critical section and it still raises the "Can not activate current context" exception.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…