Skip to content

Commit

Permalink
YDW.Threading, and other.
Browse files Browse the repository at this point in the history
YDW.Threading: found a memory leak, but not fixed yet.
Other changes: Fixed exception raising.
Tests\IWU: Fixed application terminating.
  • Loading branch information
Kisspeace committed Mar 22, 2023
1 parent 87c8b3b commit 7c336fb
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 166 deletions.
2 changes: 1 addition & 1 deletion source/YDW.FMX.ImageWithURLCacheManager.pas
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ procedure TImageWithURLCahceManager.LoadFromCache(AUrl: string;
Except on E: Exception do begin
Log('TImageWithURLCahceManager.LoadFromCache(); SyncBitmapLoadFromFile: ' +
Booltostr(SyncBitmapLoadFromFile, True), E);
Raise E;
raise;
end; end;
{$ENDIF}
end;
Expand Down
37 changes: 24 additions & 13 deletions source/YDW.FMX.ImageWithURLManager.pas
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ procedure TImageWithUrlManager.SubThreadExecute(AValue: IImageWithURL);

function IsValidHTTPURL(const AURL: string): boolean;
begin
Result := AURL.StartsWith('https://', True);
Result := AURL.StartsWith(TURI.SCHEME_HTTPS, True);
if not Result then
Result := AURL.StartsWith('http://', True);
Result := AURL.StartsWith(TURI.SCHEME_HTTP, True);
end;

function IsStoredLocal(const AURL: string): boolean;
Expand Down Expand Up @@ -163,28 +163,35 @@ procedure TImageWithUrlManager.SubThreadExecute(AValue: IImageWithURL);

begin
LImage := AValue;

LUrl := LImage.ImageURL; // Saved local url
LFinalImage := nil;

try
try
if EnableLoadFromCache and Assigned(CacheManager)
if EnableLoadFromCache
and Assigned(CacheManager)
and CacheManager.IsCached(LUrl) then begin

// Have copy on cache
CacheManager.LoadFromCache(LUrl, LImage.BitmapIWU);
LImageLoaded := True;

end else if IsStoredLocal(LUrl) then begin

// Url is file path and file exists
if SyncBitmapLoadFromFile then begin
TThread.Synchronize(TThread.Current, procedure begin

TThread.Synchronize(TThread.Current, procedure
begin
if Self.LoadThumbnailFromFile then
LImage.BitmapIWU.LoadThumbnailFromFile(LUrl, ThumbSize.Width, ThumbSize.Height) // ISSUE (FormOnCreate, FormOnShow)
else
LImage.BitmapIWU.LoadFromFile(LUrl); // ISSUE (FormOnCreate, FormOnShow)
LImageLoaded := True;
end);

end else begin // Usign buffer bitmap

LBufBmp := TBitmap.Create;
try
{$IFDEF YDW_DEBUG} try {$ENDIF}
Expand All @@ -193,23 +200,26 @@ procedure TImageWithUrlManager.SubThreadExecute(AValue: IImageWithURL);
else
LBufBmp.LoadFromFile(LUrl); // ISSUE (FormOnCreate, FormOnShow)

TThread.Synchronize(nil,
TThread.Synchronize(TThread.Current,
procedure
begin
LImage.BitmapIWU.Assign(LBufBmp);
LImageLoaded := True;
end);

{$IFDEF YDW_DEBUG}
Except
on E: Exception do begin
Log('IsStoredLocal async load', E);
Raise E;
raise;
end;
end;
{$ENDIF}

finally
LBufBmp.Free;
end;

end;

end else begin
Expand All @@ -228,19 +238,18 @@ procedure TImageWithUrlManager.SubThreadExecute(AValue: IImageWithURL);
end;

LClient.SynchronizeEvents := False;
LClient.Asynchronous := false;
LClient.Asynchronous := False;
LClient.AutomaticDecompression := [THTTPCompressionMethod.Any];

if TThread.Current.CheckTerminated then exit;

LResponse := LClient.Get(LUrl);
if TThread.Current.CheckTerminated then exit;

if not AllowThisResponse(LUrl, LResponse) then exit;

Self.EncodeImage(LResponse.ContentStream, LFinalImage);
EncodeImage(LResponse.ContentStream, LFinalImage);

if Self.EnableSaveToCache and Assigned(CacheManager) then begin
if EnableSaveToCache and Assigned(CacheManager) then begin
if not CacheManager.IsCached(LUrl) then
CacheManager.CacheItem(LUrl, LFinalImage);
end;
Expand All @@ -254,7 +263,8 @@ procedure TImageWithUrlManager.SubThreadExecute(AValue: IImageWithURL);

finally

if Assigned(LResponse) and (LFinalImage <> LResponse.ContentStream) then
if Assigned(LResponse)
and (LFinalImage <> LResponse.ContentStream) then
FreeAndNil(LFinalImage);

LClient.Free;
Expand All @@ -267,10 +277,11 @@ procedure TImageWithUrlManager.SubThreadExecute(AValue: IImageWithURL);
On E: Exception do
DoOnException(E);
end;

finally

if Assigned(LImage.OnLoadingFinished) then begin
TThread.Synchronize(nil,
TThread.Synchronize(TThread.Current,
procedure
begin
LImage.OnLoadingFinished(LImage as TObject, LImageLoaded);
Expand Down
Loading

0 comments on commit 7c336fb

Please sign in to comment.