Page 1 of 1

Postgresql BlobRead Memory Leak

Posted: 04.08.2007, 10:28
by Tom
I use zeosdbo with Postgresql 7.3. and Delphi.

I use CreateBlobStream.
Memory Leak happend.

exp)

BlobStream := CreateBlobStream(TBlobField(Fields[1]), bmRead);
BlobStream.Free;

--------------

I found problem.
-- ZDbcPostgreSqlResultSet.pas

within:procedure TZPostgreSQLBlob.ReadBlob;

ReadStrem is not released.

725: SetStream(ReadStream);

I added this code.

if ReadStream <> Nil then
ReadStream.Free;
end;

Posted: 05.08.2007, 23:51
by mdaems
Can you give a little more detail? Where exactly did you add the lines above. The function is short enough. Can you please copy/paste your version of the TZPostgreSQLBlob.ReadBlob function?

Mark

Posted: 07.08.2007, 15:46
by Tom
Sorry.

TZPostgreSQLBlob.ReadBlob is

Code: Select all

procedure TZPostgreSQLBlob.ReadBlob;
var
  BlobHandle: Integer;
  Buffer: array[0..1024] of Char;
  ReadNum: Integer;
  ReadStream: TMemoryStream;
begin
  if not Updated and (FBlobOid > 0) then
  begin
    BlobHandle := FPlainDriver.OpenLargeObject(FHandle, FBlobOid, INV_READ);
    CheckPostgreSQLError(nil, FPlainDriver, FHandle, lcOther, 'Read Large Object',nil);
    if BlobHandle >= 0 then
    begin
      ReadStream := TMemoryStream.Create;
      repeat
        ReadNum := FPlainDriver.ReadLargeObject(FHandle, BlobHandle,
          Buffer, 1024);
        if ReadNum > 0 then
        begin
          ReadStream.SetSize(ReadStream.Size + ReadNum);
          ReadStream.Write(Buffer, ReadNum);
        end;
      until ReadNum < 1024;
      FPlainDriver.CloseLargeObject(FHandle, BlobHandle);
      ReadStream.Position := 0;
    end else
      ReadStream := nil;
    SetStream(ReadStream);

    if ReadStream <> Nil then
        ReadStream.Free;

  end;
end;

ReadStream was Created, But is not Free.

Will I be wrong?

Posted: 08.08.2007, 10:30
by cipto_kh
if ReadStream <> Nil then
ReadStream.Free;

I think it should be:
if ReadStream <> Nil then
FreeAndNil(ReadStream);

Becasue the Free mothod didn't nil the instance.

Posted: 26.11.2009, 22:17
by mdaems
Hi,

Thanks for reporting again, in the bug tracker this time.

I did apply the patch now. (in the easy version as you wrote it above, not an exact copy from the bug tracker)

Cipto, FreeAndNil is not necessary as the variable is only local and destroyed a few lines later at the end of the procedure.

Mark