Spelt / ZXing.Delphi

ZXing Barcode Scanning object Pascal Library for Delphi VCL and Delphi Firemonkey
Apache License 2.0
471 stars 206 forks source link

Memory leaks in TInvertedLuminanceSource when hints contains ZXing.DecodeHintType.ENABLE_INVERSION and ZXing.DecodeHintType.TRY_HARDER #172

Open JedrzejczykRobert opened 2 months ago

JedrzejczykRobert commented 2 months ago

In functions: crop, rotate

Self.delegate.crop(left, top, width, height)
Self.delegate.rotateCounterClockwise
Self.delegate.rotateCounterClockwise4

returns new instance of TLuminanceSource, it should be stored and released in TInvertedLuminanceSource.Destructor;

suggested changes:

constructor TInvertedLuminanceSource.Create(delegate: TLuminanceSource; ownDelegate : Boolean = FALSE);
begin
  inherited Create(delegate.Width, delegate.Height);
  Self.delegate := delegate;
  Self.ownDelegate := ownDelegate; // new property
end;

destructor TInvertedLuminanceSource.Destroy;
begin
  if Self.ownDelegate then
  begin
    FreeAndNil(Self.delegate);
  end;
  inherited;
end;

function TInvertedLuminanceSource.crop(const left, top, width, height: Integer): TLuminanceSource;
begin
  Result := TInvertedLuminanceSource.Create(
    Self.delegate.crop(left, top, width, height),
    TRUE);
end;

function TInvertedLuminanceSource.rotateCounterClockwise(): TLuminanceSource;
begin
  Result := TInvertedLuminanceSource.Create(
    Self.delegate.rotateCounterClockwise,
    TRUE);
end;

function TInvertedLuminanceSource.rotateCounterClockwise45(): TLuminanceSource;
begin
  Result := TInvertedLuminanceSource.Create(
    Self.delegate.rotateCounterClockwise45,
    TRUE);
end;

// should return new instance like TBaseLuminanceSource.Invert()
// not optimal, but consistent with the crop and rotate functions
function TInvertedLuminanceSource.invert: TLuminanceSource;
begin
  Result := TInvertedLuminanceSource.Create(SELF);
end;