Recent

Author Topic: External SIGFPE from TSQLQuery  (Read 6897 times)

cov

  • Full Member
  • ***
  • Posts: 241
External SIGFPE from TSQLQuery
« on: July 23, 2013, 08:12:47 am »
Hi,

This may, or may not be related to this thread, but I'm getting an external SIGFPE on my program when I do a SQLQuery.Close;

Code: [Select]
  ID: string;
  b: TStream;
begin
  ID:=Form_DB.Datasource1.DataSet.FieldByName('ID').Value;
  SQLQuery1.Close;
  SQLQuery1.SQL.Text:='SELECT * FROM PREVIEWS WHERE ID='+ID;
  SQLQuery1.Open;
  if SQLQuery1.IsEmpty then
  begin
    getODDrawing(ID);
    SQLQuery1.Close; //<--- SIGFPE Here
  end;
end;

procedure TForm_DB.getODDrawing(ID: String);
var
  BlkHandle: String;
  R: TRect;
  strm: Tstream;
begin
  if BlobList=nil then BlobList:= TStringList.Create;//<---- BlobList is global TStringList
  currentRecord:=ID;
  SQLQuery1.Close;
  SQLQuery1.SQL.Text:='SELECT PATHID,HANDLE FROM DWGMAP WHERE FIELDID='+ID;
  SQLQuery1.Open;
  ID:=SQLQuery1.FieldByName('PATHID').AsString;
  BlkHandle:=SQLQuery1.FieldByName('HANDLE').AsString;
  SQLQuery1.Close;
  SQLQuery1.SQL.Text:='SELECT ACCESSED FROM PATHS WHERE ID='+ID;
  SQLQuery1.Open;
  ID:=SQLQuery1.FieldByName('ACCESSED').AsString;
  SQLQuery1.Close;
  SQLQuery1.SQL.Text:='SELECT ODDRAWING FROM USERSACCESS WHERE ID='+ID;
  SQLQuery1.Open;
  strm:=SQLQuery1.CreateBlobStream(SQLQuery1.Fields[0],bmRead);
  BlobList.LoadFromStream(strm);
  unit_display.showOD(BlkHandle);
  R:=Rect(0,0,ImageWidth,ImageHeight);
  Image1.Canvas.CopyRect(R,ImageBuffer.Canvas,R);
  Screen.Cursor:=crDefault;
  strm.Free;
end;                                       
 

If it's the first record then it works fine. But if it's the second record (ie, another record has already been processed) then the SIGFPE occurs. Possible problem with the BlobList?

The SIGFPE opens an Assembler window which contains the following snippet:

Code: [Select]
5AD7258E 3bc7                     cmp    %edi,%eax
5AD72590 897df8                   mov    %edi,-0x8(%ebp)
5AD72593 7409                     je     0x5ad7259e
5AD72595 844838                   test   %cl,0x38(%eax)
5AD72598 0f851bd60100             jne    0x5ad8fbb9 <UxTheme!GetThemeTextMetrics+8319>
5AD7259E 8b4508                   mov    0x8(%ebp),%eax
5AD725A1 d9e8                     fld1   <- Exception raised at 5AD725A1

The showOD() procedure steps through the blob list and draws on my ImageBuffer canvas. There is quite a lot of calculation done as each line in the BlobList contains a series of comma-separated points which must be split into Doubles and then scaled to fit and then truncated to integers before being rendered on the Imagebuffer canvas.

I realise that my coding is not all that elegant and my SQL is a little clunky, but any suggestions would be welcome.

~ Dave
« Last Edit: July 23, 2013, 08:50:32 am by cov »

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: External SIGFPE from TSQLQuery
« Reply #1 on: July 23, 2013, 08:25:42 am »
Try:
--
ID:=Form_DB.Datasource1.DataSet.FieldByName('ID').AsInteger;//also set ID : Integer--var
---
SQLQuery1.Close;
//clear text
SQLQuery1.SQL.Clear;//clear current query

    SQLQuery1.SQL.Text:='SELECT * FROM PREVIEWS WHERE ID='+ID;
//For string fields: QuotedStr(value)
« Last Edit: July 23, 2013, 08:28:52 am by exdatis »

cov

  • Full Member
  • ***
  • Posts: 241
Re: External SIGFPE from TSQLQuery
« Reply #2 on: July 23, 2013, 08:39:28 am »
Thanks for the reply.

ID field is integer, so that is okay. The ID variable is a string, so that should be alright too.

The SQLQuery1.SQL.Clear; doesn't really have any impact and the Exception is raised at the same point.

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: External SIGFPE from TSQLQuery
« Reply #3 on: July 23, 2013, 08:43:29 am »
ID : String
I still think it's a mistake, try to change(ID : Integer).
Regards
p.s. Clear query is just a good practice

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: External SIGFPE from TSQLQuery
« Reply #4 on: July 23, 2013, 02:02:04 pm »
First, SIGFPE exceptions are raised on the next FPU instruction. So the pointer you get is not the place were it went wrong. It went wrong in the previous FPU instruction.  Second, UxTheme!GetThemeTextMetrics is in an external dll. Delphi, and thus Freepascal, have a non standard FPEMask. A lot of C libraries don't use exceptions to check FPU problems but inspect the FPU status. Try changing the mask with SetExceptionMask (http://www.freepascal.org/docs-html/rtl/math/setexceptionmask.html) as early as possible in your program.

cov

  • Full Member
  • ***
  • Posts: 241
Re: External SIGFPE from TSQLQuery
« Reply #5 on: July 23, 2013, 03:05:04 pm »
Hi, ludob,

Can you point me to how to do that?

I'm sure you're right, there isn't really much to cause an exception in the SQLQuery.

The drawing record is a List of points which is rendered onto an imagebuffer. This is rendered on a TImage canvas and also placed into the database as a bitmap.

This particular drawing record is rendered and the bitmap inserted without error if it is selected first. If, however, it is selected after another record has been rendered and inserted, then the exception is generated.

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: External SIGFPE from TSQLQuery
« Reply #6 on: July 23, 2013, 03:18:38 pm »
Code: [Select]
uses  math;
...
 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision])
That will turn off all FPU exceptions.

If you think there is something wrong in the FPC code, you can use the $SAFEFPUEXCEPTIONS (http://www.freepascal.org/docs-html/prog/progsu69.html ) directive. This will add a FWAIT after every store of a FPU value to memory. Most FPC float and double operations end with storing the result somewhere in memory so the outcome is that the debugger stops on the correct FPC line. FWAIT is a nop for the FPU but will cause an exception to be raised immediately instead of somewhere down your code.  It is not done per default because it slows down floating point operations a lot.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: External SIGFPE from TSQLQuery
« Reply #7 on: July 24, 2013, 07:55:50 am »
Added info to
http://wiki.lazarus.freepascal.org/GDB_Debugger_Tips#SIGFPE
... couldn't find a better spot to put it...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018