On a windows 10 system with 16GB ram, my program (which uses under 500mb ram at most) randomly throws "Not enough memory resources are available to process this command" exceptions when decoding JPEG files (ones I created using code). The JPEG files are valid and load without issue when calling the same function again.
The JPEG loading code is run in a separate thread than the main UI and the exception is always in the same spot (LoadedImage.Height := gdiBitmap.GetHeight), here is the code (loads JPEG from Stream):
function LoadJPEGImageGDIPlus(lStream : TStream; LoadedImage : TBitmap; LoadEXIF : Boolean{$IFDEF LOCALTRACE}; ThreadID : WideString{$ENDIF}) : Boolean;
var
gdiBitmap : TGPBitmap;
gfx : TGPGraphics;
I : Integer;
gdiStatus : TStatus;
begin
Result := True;
gfx := nil;
gdiBitmap := nil;
LoadedImage.Canvas.Lock;
Try
gdiBitmap := TGPBitmap.Create(TStreamAdapter.Create(lStream));
LoadedImage.PixelFormat := pf32bit;
LoadedImage.Width := gdiBitmap.GetWidth;
LoadedImage.Height := gdiBitmap.GetHeight;
I := 0;
Repeat
gfx := TGPGraphics.Create(LoadedImage.Canvas.Handle);
gdiStatus := gfx.GetLastStatus;
If gdiStatus <> ok then
Begin
FreeAndNil(gfx);
Inc(I);
Sleep(16);
End;
Until (gdiStatus = ok) or (I > 150);
If gdiStatus = ok then
Begin
gdiStatus := gfx.DrawImage(gdiBitmap,0,0,LoadedImage.Width,LoadedImage.Height);
If gdiStatus <> ok then
Begin
Result := False;
End;
FreeAndNil(gfx);
End
else
Begin
Result := False;
End;
Except
on E : Exception do
Begin
Result := False;
End
else
Begin
Result := False;
End;
End;
If gdiBitmap <> nil then FreeAndNil(gdiBitmap);
If gfx <> nil then FreeAndNil(gfx);
LoadedImage.Canvas.Unlock;
end;
Any idea how to resolve this issue, am I not using GDI+ correctly?