Board index » delphi » Loading an Icon from the Database wont free all resources

Loading an Icon from the Database wont free all resources

Using this simple proc bellow result with still allocated resources realated
to the
 line "Icon.LoadFromStream(BlobStream);". when this line removed resourced
are deallocated properly.
("EmpJob1" is a table with a Blob field containing an Icon)

(The resource meter used: MemProof and AQTime)

procedure TForm1.BitBtn1Click(Sender: TObject);
var
   Qry        : TQuery;
   icon       : TIcon;
   BlobStream : TBlobStream;
begin
  Qry := TQuery.Create(nil);
  Qry.DataBaseName := 'office';
  Qry.Sql.Clear;
  Qry.Sql.Add('Select * From EmpJob1');
  Qry.Open;
  Qry.First;
  Icon := TIcon.Create;
  try
    BlobStream := TBlobStream.Create(Qry.FieldByName('JOB_ICON') as
TBlobField, bmRead);
    Icon.LoadFromStream(BlobStream);
  except
  end;
  Icon.Free;
  BlobStream.Free;
  Qry.Close;
  Qry.Free;
end;

Thanks
Chen.

 

Re:Loading an Icon from the Database wont free all resources


Quote
"Chen Mark" <chenm...@ZAHAV.Net.IL> wrote in message

news:3c1edce6_1@dnews...

Quote
> Using this simple proc bellow result with still allocated resources
realated
> to the
>  line "Icon.LoadFromStream(BlobStream);". when this line removed resourced
> are deallocated properly.
> ("EmpJob1" is a table with a Blob field containing an Icon)

> (The resource meter used: MemProof and AQTime)
[snip]
>   Qry := TQuery.Create(nil);
>   Qry.DataBaseName := 'office';
>   Qry.Sql.Clear;
>   Qry.Sql.Add('Select * From EmpJob1');
>   Qry.Open;
>   Qry.First;
>   Icon := TIcon.Create;
>   try
>     BlobStream := TBlobStream.Create(Qry.FieldByName('JOB_ICON') as
> TBlobField, bmRead);
>     Icon.LoadFromStream(BlobStream);
>   except
>   end;
>   Icon.Free;
>   BlobStream.Free;
>   Qry.Close;
>   Qry.Free;
> end;

In the case where everything executes properly without error, there should
not be any resource leaks with the above code and I would say the memory
checkers are reporting wrong (or they are reporting *potential* rather than
actual leak).

However there are a few things that should be changed. First, it is possible
to get an exception on the opening of the query, in this case the TQuery
will not be deallocated. Second, you should never use a try/except block to
completely suppress errors as you are doing above. You should either perform
some work in the except block (and optionally rereaise it), or you should
remove it and let the exception bubble up.

You should use a try/finally block to guarantee cleanup of resources:

   Qry := TQuery.Create(nil);
    try
       Qry.DataBaseName := 'office';
       Qry.Sql.Clear;
       Qry.Sql.Add('Select * From EmpJob1');
       Qry.Open;
       Qry.First;
       Icon := TIcon.Create;
       try
           BlobStream := TBlobStream.Create(Qry.FieldByName('JOB_ICON') as
           TBlobField, bmRead);
           Icon.LoadFromStream(BlobStream);
       finally
           Icon.Free;
           BlobStream.Free;
       end;
       Qry.Close;
  finally
   Qry.Free;
 end;

See if thihs gets reported any differently.

--
Wayne Niddery (Logic Fundamentals, Inc.)
RADBooks: http://www.logicfundamentals.com/RADBooks/delphibooks.html
Try to make original mistakes, rather than needlessly repeating [others]. -
Donald Rumsfeld, U.S. Secretary of Defense

Other Threads