Recent

Author Topic: TDBImage TBitmap Load - Heap overflow error  (Read 2430 times)

Int3g3r

  • New Member
  • *
  • Posts: 17
TDBImage TBitmap Load - Heap overflow error
« on: May 03, 2024, 04:47:56 pm »
Hello

I get a heap overflow on loading a Bitmap with TDBImage.
Storing the bitmap in the tdbf works fine.
if i use jpeg loading works fine, any other format it crashes.

installed packages: LazBarcodes, QRcodeGenLib

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,
  3.   ComCtrls, DBGrids, DBCtrls, ubarcodes, LR_PGrid,
  4.   DB, BufDataset, dbf;  
  5.  
  6. .....
  7.  
  8. procedure TfrmMain.FormCreate(Sender: TObject);
  9. begin
  10.   ApplicationPath:= ExtractFileDir(Paramstr(0));
  11.   DbName:= 'db.dbf';
  12.  
  13.  with dbFile do begin
  14.    FilePath:= ApplicationPath;
  15.    OpenMode:= omAutoCreate;
  16.    Storage:=stoFile;
  17.    StoreDefs:=True;
  18.    TableLevel:=7;
  19.    TableName:= DbName;
  20.    FieldDefs.Add('ArtNr',ftString,30);
  21.    FieldDefs.Add('Firma',ftString,30);
  22.    FieldDefs.Add('Bezeichnung',ftString,30);
  23.    FieldDefs.Add('Einsatz',ftString,30);
  24.    FieldDefs.Add('Lagerort',ftString,30);
  25.    FieldDefs.Add('qrImage',ftGraphic);
  26.    dbFile.Open;
  27.    //FieldByName('qrImage').Visible:= false;
  28.  end;
  29.  
  30.  imageView.DataSource := dsMem;
  31.  imageView.DataField:= 'qrImage';
  32. end;
  33.  
  34. procedure TfrmMain.dbFileBeforePost(DataSet: TDataSet);
  35. var myStream: TMemoryStream;
  36. begin
  37.   qrGen.Text := Edit1.Text;
  38.   qrGen.Generate;
  39.   myStream := TMemoryStream.Create;
  40.   qrGen.SaveToStream(myStream,TBitmap,500,500);
  41.   try
  42.     dbFile.FieldByName('qrImage').Clear;
  43.     myStream.Position:=0;
  44.     TBlobField(dbFile.FieldByName('qrImage')).LoadFromStream(myStream);
  45.   finally
  46.     myStream.Free;
  47.   end;
  48. end;      
  49.  



Regards Int3g3r
« Last Edit: May 03, 2024, 04:50:03 pm by Int3g3r »

wp

  • Hero Member
  • *****
  • Posts: 12682
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #1 on: May 03, 2024, 07:12:21 pm »
As far as I know, TDbf does not support the ftGraphic field type correctly. If you create the field as ftBLOB and use the following code for loading/saving intoa standard TImage, it works:
Code: Pascal  [Select][+][-]
  1. // Extracts an image from the database field with the name APictureFieldName and displays it in the given picture (e.g. Image1.Picture)
  2. procedure TMainDatamodule.LoadPicture(APictureFieldName: String; APicture: TPicture);
  3. var
  4.   stream: TStream;
  5. begin
  6.   stream := Dbf1.CreateBlobStream(Dbf1.FieldByName(APictureFieldName), bmRead);
  7.   try
  8.     if stream.Size = 0 then
  9.     begin
  10.       APicture.Clear;
  11.       exit;
  12.     end;
  13.     stream.Position := 0;
  14.     APicture.LoadFromStream(stream);
  15.   finally
  16.     stream.Free;
  17.   end;
  18. end;
  19.  
  20. // Stores the provided picture (e.g. Image1.Picture) in the database field FIconField.
  21. procedure TMainDatamodule.StorePicture(PictureFieldName: String; APicture: TPicture);
  22. var
  23.   stream: TStream;
  24. begin
  25.   stream := TMemoryStream.Create;
  26.   try
  27.     APicture.SaveToStream(stream);
  28.     stream.Position := 0;
  29.     TBlobField(Dbf1.FieldByName(PictureFieldName)).LoadFromStream(stream);
  30.   finally
  31.     stream.Free;
  32.   end;
  33. end;

If you have a TDBImage on the form you must replace it by a TImage. And you must provide handlers for database events, at least for AfterScroll and AfterOpen, to update the TImage when needed with the current record's picture by calling LoadPicture(Image1.Picture)

See the attachment for a simple worked-out example (Note that you must adjust the constant LAZARUS_DIR to the location of your lazarus installation; if your Laz version is too old you must also replace the "general_purpose" by another directory in the images folder of the installation).

« Last Edit: May 03, 2024, 07:39:53 pm by wp »

jamie

  • Hero Member
  • *****
  • Posts: 6823
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #2 on: May 04, 2024, 01:06:35 am »
isn't a Blob Field limited to a size? Seems I remember it once being 255 bytes.
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 12682
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #3 on: May 05, 2024, 12:24:00 am »
No. BLOB = binary large object. Why would there be a "large" in the name if it were restricted to 255 bytes only?

jamie

  • Hero Member
  • *****
  • Posts: 6823
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #4 on: May 05, 2024, 12:46:07 am »
BLOB in my days meant unknown type of DATA just thrown in there and there was a size limit.

Maybe they have moved that up a but in the old days I used a Database components in Delphi and the Blob has a limited size.

 Anything over that, you had to use multiple Blobs to store a single item.



The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 4141
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #5 on: May 05, 2024, 01:10:57 am »
Almost Jamie  :)

Blobs where stored not in the database itself but in a separate file (dbt ?). That file stored data into blocks and each block had a limit (I can't recall exactly but it was 255, 512 or 1024 bytes). Therefor each blob consisted out of a number of blocks in that file and overall that had a limit because the index to these blocks was limited (it was a couple of tens of MB's if I recall correctly). Ofc. things improved further down the line but personally I lost track.
Today is tomorrow's yesterday.

Int3g3r

  • New Member
  • *
  • Posts: 17
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #6 on: May 06, 2024, 08:21:51 am »
As far as I know, TDbf does not support the ftGraphic field type correctly.

Thank you for the example.  :)

Is there an alternative to tdbf that works properly?
Or would you use sqlite right away?

My application only needs one table that I want to print with lazreport. I do not need referential integrity.

cdbc

  • Hero Member
  • *****
  • Posts: 1953
    • http://www.cdbc.dk
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #7 on: May 06, 2024, 08:33:43 am »
Hi
Quote
Or would you use sqlite right away?
YES!
Hands down. it's a much better /single-user/ database & sooo easy to work with  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

Zvoni

  • Hero Member
  • *****
  • Posts: 2897
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #8 on: May 06, 2024, 08:40:10 am »
Or would you use sqlite right away?

My application only needs one table that I want to print with lazreport. I do not need referential integrity.
Hell Yes.

The only downside compared to DBF: You have to "ship" the dll, but that's no skin of anyones nose.
And even the "shipping of the dll" should be avoidable, if anyone figures out how to link the sqlite-lib fully static into your code (and i'm close to figuring it out)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 16652
  • Kallstadt seems a good place to evict Trump to.
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #9 on: May 06, 2024, 09:38:05 am »
The only downside compared to DBF: You have to "ship" the dll, but that's no skin of anyones nose.
And even the "shipping of the dll" should be avoidable, if anyone figures out how to link the sqlite-lib fully static into your code (and i'm close to figuring it out)
Not if you link statically, like mORMot can... No dll's.
But I am sure they don't want the Trumps back...

Zvoni

  • Hero Member
  • *****
  • Posts: 2897
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #10 on: May 06, 2024, 09:59:11 am »
The only downside compared to DBF: You have to "ship" the dll, but that's no skin of anyones nose.
And even the "shipping of the dll" should be avoidable, if anyone figures out how to link the sqlite-lib fully static into your code (and i'm close to figuring it out)
Not if you link statically, like mORMot can... No dll's.
that's what i meant: Link sqlite fully statically into your project WITHOUT mORMot.
This requires a rework of sqlite3.inc and sqlite3conn as far as i can see
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

paweld

  • Hero Member
  • *****
  • Posts: 1323
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #11 on: May 06, 2024, 12:16:36 pm »
As you only need one table then you can also use TBufDataSet. In the attachment you have examples with TBufDataSet and SQLite.
Best regards / Pozdrawiam
paweld

Int3g3r

  • New Member
  • *
  • Posts: 17
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #12 on: May 08, 2024, 01:56:14 pm »
I have just created a new project.
This time with an SQLite3 database.
Had exactly the same problem.  :o

Here I have found the solution:
https://forum.lazarus.freepascal.org/index.php?topic=30208.0

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain.FormCreate(Sender: TObject);
  2. begin
  3.   imageView.WriteHeader:= false;
  4. end;
  5.  
  6. procedure TfrmMain.imageViewDBImageRead(Sender: TObject; S: TStream;
  7.   var GraphExt: string);
  8. begin
  9.   GraphExt:= 'bmp';
  10. end;  
  11.  

The image component does not know what format the image is saved in.
It is probably opened as a jpeg because no format is specified.
This would also explain why loading a jpeg is not a problem.
So the error does not come from the TDBF database.

Now both projects work.  :)

wp

  • Hero Member
  • *****
  • Posts: 12682
Re: TDBImage TBitmap Load - Heap overflow error
« Reply #13 on: May 08, 2024, 03:01:14 pm »
Good catch!

 

TinyPortal © 2005-2018