External SIGFPE from TSQLQuery lazarus / 16-Databases ---------------------------------------------------------------------- cov · 2013-07-23 06:12 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 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 ---------------------------------------------------------------------- exdatis · 2013-07-23 06:25 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) ---------------------------------------------------------------------- cov · 2013-07-23 06:39 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 · 2013-07-23 06:43 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 · 2013-07-23 12:02 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 · 2013-07-23 13:05 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 · 2013-07-23 13:18 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 · 2013-07-24 05:55 Added info to http://wiki.lazarus.freepascal.org/GDB_Debugger_Tips#SIGFPE ... couldn't find a better spot to put it...