[Solved] FBTemp Crashing when trying to access Database

General FreeBASIC programming questions.
Post Reply
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

[Solved] FBTemp Crashing when trying to access Database

Post by Gablea »

Hi Everyone,

Could some one please help I am stuck and I am not sure what to do as I am still learning MySQL access with FreeBASIC

this is my code I am using

Code: Select all

	TotalCol = 0
   TotalRow = 0
		  CiR = 0
		  CiC = 0
    
	MyQuery = ""
	MyQuery += "select "
	MyQuery += "userid, "
	MyQuery += "username "
	MyQuery += "from usertable "
	MyQuery += "WHERE "
	MyQuery += "userid='" & CashierNumber &"';"	
	
	Print MyQuery
		
	Conn = mysql_init(NULL)

   If (Conn = 0) then 
   	Print "Error"
   Else
   	Print "Conn ok"
   EndIf
   
   If mysql_real_connect(Conn, ServerIP, ServerUserName, ServerUserPassword, ServerDatabaseName, Val(ServerPortNumber), NULL, 0) = NULL Then
    	Print "Error Connecting to Database"
    	Print ServerIP
    	Print ServerUserName
    	Print ServerUserPassword
    	Print ServerDatabaseName
    	Print Val(ServerPortNumber)
   Else
   	Print "Connection Estabablished"
   EndIf

   If mysql_query(Conn, MyQuery) then Print "Error"

   MySQLOut = mysql_store_result(Conn)

   If MySQLOut = 0 then Print "Error"
    
   TotalCol = mysql_num_fields(MySQLOut)
   TotalRow = mysql_num_rows(MySQLOut)
    
	If debugmode = 1 Then print TotalRow
	
	
	Select Case TotalRow
		Case 0 ' Nothing found
	    	mysql_free_result(MySQLOut)
   	 	mysql_close(Conn)
			MessageToDisplay("Cashier_Error", "Cashier not found", "Sorry your user ID cound not", "be found Please check and", "try your number again","Press clear to carry on","","","")
			Exit Sub
	
		Case 1 ' Record Found
				   CashierNumber = *Row[0]
			 CashierNameDisplay = *Row[1]
					  KeyPadInput = ""
		    mysql_free_result(MySQLOut)
		    mysql_close(Conn)
			SignOnScreen_Password
			Exit Sub
	End Select
but when I try to run the code from with in my application I get the following screen

Image

I am not getting any errors print on the debug screen (unless I am using the wrong code to print the debug screen)

What should happen is I enter the user ID of 1105 and then it should get the user details from the database and show the User name and ask for the Password.

I am sorry if i keep asking questions on my PoS application I am still figuring this out (its a lot different then Windows)
Last edited by Gablea on Jul 25, 2022 15:36, edited 1 time in total.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FBTemp Crashing when trying to access Database

Post by caseih »

I assume you're compiling with the -g and -exx flags? If you click on "debug" you should at least get a stack trace when it crashes like that. That can help you narrow down the source of the crash.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: FBTemp Crashing when trying to access Database

Post by Gablea »

caseih wrote: Jul 24, 2022 3:25 I assume you're compiling with the -g and -exx flags? If you click on "debug" you should at least get a stack trace when it crashes like that. That can help you narrow down the source of the crash.
Er no I did not know that was a compile option. I'll try that and post the results
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: FBTemp Crashing when trying to access Database

Post by Gablea »

Well I have complied it like this fbc g -exx but th application quits the moment it is ran

have I done something to it to kill it?
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: FBTemp Crashing when trying to access Database

Post by angros47 »

Without the source code, we cannot replicate the issue, so we can't answer. Why don't you place some PRINT instructions in your application, at each step of initialization, so you can see how long it goes before crashing (and locate the instruction that causes the issue)?
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: FBTemp Crashing when trying to access Database

Post by Gablea »

angros47 wrote: Jul 24, 2022 21:59 Without the source code, we cannot replicate the issue, so we can't answer. Why don't you place some PRINT instructions in your application, at each step of initialization, so you can see how long it goes before crashing (and locate the instruction that causes the issue)?
I have tried that but it is not even initialization the graphical part of my app it starts and then quits right away

Code: Select all

	' © 2014 - 2022, algPoS, Northampton 
	'           Coded by : Andrew Gable
	' 	      Desgined by : Andrew Gable
	'  Software owned by : Andrew Gable and algPoS, Northampton
	' This version USES THE MySQL interface to a local Database that is Kept in sync with the back office via DataSync Program
	' This is currently written in Gambas 3 (AT some point ALL Linux appliations are moving to Gambas 3 ONCE i work out how to
	' Create the linux version of the promtions DLL)
	
	'Include Files
	#Include Once "mysql\mysql.bi"
	#include Once "declaresubs.bi"
	
	'Defines
	#Define NULL 0

	Using FB 


	Dim StartOption As string = *__FB_ARGV__[1]	' either normal or fullscreen or console if booting from the text only interface

	Select Case StartOption
		Case "normal", "Normal", ""
			'Screen 19, 32, 2, (GFX_WINDOWED Or GFX_NO_FRAME Or fb.GFX_ALWAYS_ON_TOP)
			ScreenRes 800, 600, 32, 2, (GFX_WINDOWED Or GFX_NO_FRAME Or fb.GFX_ALWAYS_ON_TOP)
			SetMouse ,,0

		Case "fullscreen", "Fullscreen"
			'Screen 19, 32, 2, (GFX_NO_FRAME or GFX_NO_SWITCH Or fb.GFX_FULLSCREEN)
			ScreenRes 800, 600, 32, 2, (GFX_NO_FRAME Or fb.GFX_ALWAYS_ON_TOP Or fb.GFX_FULLSCREEN)
			SetMouse ,,0
	End Select
		
			 ApplicationVersion = "6.0.0.1"	'change me when ever a new function is active and STABLE
						 DebugMode = 1
							    ESC = Chr$(&H1B)
					 DebugConsole = 22
					     SaleMode = "Sale"
 	    CashierOverrideAccess = 0
		  			 DisplayWidth = 62
		   		 RecipitClear = "Yes"
			  DisplayTenderIcon = "£"
							ItemQty = 1
	 			ShowTaskBarItems = 0
 	 			 SubTotalPressed = 0
	 		 MenuBeingDisplayed = "None"
	 		PrinterRecipitWidth = 56 	'Set the Width Ready to load the data in from the Settings.ini file

	'Remove this option once card intergration has been done
			  'StoreMerchentSlip = 0 ' DO not store
	    		'	BarcodeRefund = 0
   	  			 StoreEFTSlip = 0	' No EFT Slip to store

' Start Of program
	' Boot up
	declare Sub StartUp
		StartUp	
This is the StartUp Function

Code: Select all

Private Sub StartUp
	
	StartupScreenBackground("BootUp")

	Sleep 1000
	
	
	UpdateScreen_Loading("Loading PoS settings file","BootUp")
		LoadPoSSettings

	UpdateScreen_Loading("Loading Printer Language file","BootUp")
		LoadPrinterLanguae
		
	Select Case RecipitOption
		Case "Always"
			UpdateScreen_Loading("Recipit is set to print at end of the transaction","BootUp")
						
		Case "Request"
			UpdateScreen_Loading("Recipit is set to print only when required","BootUp")
	End Select
	
					
	UpdateScreen_Loading("Loading PoS tender figures","BootUp")
		LoadPoSFigures
		
	UpdateScreen_Loading("Loading PoS transaction number","BootUp")
		LoadTransNumber
		UpdateScreen_Loading("Next transaction number is : " & PoSTransacationNumber,"BootUp")
		
	UpdateScreen_Loading("Initializing Hardware connections","BootUp")
	UpdateScreen_Loading("  Initializing Printer Connection","BootUp")
		EstablishPrinterConnection
				
		Select Case CustomerDisplayInterface
		Case "COM", "Com", "com"
				UpdateScreen_Loading("  Initializing Customer Display","BootUp")
					EstablishCustomerDisplayConnection
						If CustomerDisplayType = "NCR" Then SelectCustomerMessage("NCRStartUp", "",0, "", 0)
						PleaseWaitMessage
				
			Case "ViaPrinter"
				UpdateScreen_Loading("  Initializing Customer Display","BootUp")
					Print #PoSPrinter, Chr$(&H1B); "="; Chr$(2);
					Print #PoSPrinter, Chr$(&H1B); "@";
					Print #PoSPrinter, Chr$(&H1B);"R"; CHR$(3);
					Print #PoSPrinter, Chr$(&H1B); "US"; Chr$(0);																		'Turn off Line
					SelectCustomerMessage("PleaseWait","",0,"",0)
		End Select
		
		Select Case ScannerType
			Case "COM", "com", "Com"
				UpdateScreen_Loading("  Initializing Serial Barcode Reader","BootUp")
					EstablishScannerConnection
					
			'Case "USB"
			'	UpdateScreen_Loading("  USB Barcode Reader settings loaded","BootUp")
			'		EstablishScannerConnection
				
		End Select

			
	If RecipitPrintTest = "Yes" Then 
		UpdateScreen_Loading("  Printing Test Recipit....","BootUp")
		TestPagePrint
	EndIf

	ImageDestroy(BackGround_Loading)
	ClearScreenList
	SignedOffScreen		
End Sub
Before adding the -exx flag the software would boot and run
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: FBTemp Crashing when trying to access Database

Post by angros47 »

Code: Select all

	' © 2014 - 2022, algPoS, Northampton 
	'           Coded by : Andrew Gable
	' 	      Desgined by : Andrew Gable
	'  Software owned by : Andrew Gable and algPoS, Northampton
	' This version USES THE MySQL interface to a local Database that is Kept in sync with the back office via DataSync Program
	' This is currently written in Gambas 3 (AT some point ALL Linux appliations are moving to Gambas 3 ONCE i work out how to
	' Create the linux version of the promtions DLL)
PRINT "Before include"	
	'Include Files
	#Include Once "mysql\mysql.bi"
	#include Once "declaresubs.bi"
PRINT "After include"	
	
	'Defines
	#Define NULL 0

	Using FB 


	Dim StartOption As string = *__FB_ARGV__[1]	' either normal or fullscreen or console if booting from the text only interface
PRINT "Now setting screen mode"	

	Select Case StartOption
		Case "normal", "Normal", ""
			'Screen 19, 32, 2, (GFX_WINDOWED Or GFX_NO_FRAME Or fb.GFX_ALWAYS_ON_TOP)
			ScreenRes 800, 600, 32, 2, (GFX_WINDOWED Or GFX_NO_FRAME Or fb.GFX_ALWAYS_ON_TOP)
			SetMouse ,,0

		Case "fullscreen", "Fullscreen"
			'Screen 19, 32, 2, (GFX_NO_FRAME or GFX_NO_SWITCH Or fb.GFX_FULLSCREEN)
			ScreenRes 800, 600, 32, 2, (GFX_NO_FRAME Or fb.GFX_ALWAYS_ON_TOP Or fb.GFX_FULLSCREEN)
			SetMouse ,,0
	End Select
PRINT "Screen mode should be set now"	

		
And so on.

Also, might your issue be related to this one? https://freebasic.net/forum/viewtopic.php?t=31545
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FBTemp Crashing when trying to access Database

Post by caseih »

It also helps to run your app from the cmd window so you can see any messages. With -exx it should print something to the console before crashing. If you merely double-click the app it will open and close the console window so quickly you won't see anything.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: FBTemp Crashing when trying to access Database

Post by Gablea »

caseih wrote: Jul 25, 2022 3:47 It also helps to run your app from the cmd window so you can see any messages. With -exx it should print something to the console before crashing. If you merely double-click the app it will open and close the console window so quickly you won't see anything.
Hi Caseih,

Thank you for that I did not know that was a option (i found 2 bugs that I did not know about)

do you know what it means when i get the following error

Code: Select all

Aborting due to runtime error 7 (null pointer access) at line 58 of c:\algPoS\NPoS\database.bi::FINDCASHIER()
so line 58 is CashierNumber = *Row[0]

I have declared the cashiernumber as string


Below is the full sub

Code: Select all

Public Sub FindCashier(ByVal CashierNumber As String)
	TotalCol = 0
   TotalRow = 0
		  CiR = 0
		  CiC = 0
    
	MyQuery = ""
	MyQuery += "select "
	MyQuery += "userid, "							'0
	MyQuery += "username "							'1
	MyQuery += "from usertable "
	MyQuery += "WHERE "
	MyQuery += "userid='" & CashierNumber &"';"	
	
	Print MyQuery
		
	Conn = mysql_init(NULL)

   If (Conn = 0) then 
   	Print "Error"
   Else
   	Print "Conn ok"
   EndIf
   
   If mysql_real_connect(Conn, ServerIP, ServerUserName, ServerUserPassword, ServerDatabaseName, Val(ServerPortNumber), NULL, 0) = NULL Then
    	Print "Error Connecting to Database"
    	Print ServerIP
    	Print ServerUserName
    	Print ServerUserPassword
    	Print ServerDatabaseName
    	Print Val(ServerPortNumber)
   Else
   	Print "Connection Estabablished"
   EndIf

   If mysql_query(Conn, MyQuery) then Print "Error"

   MySQLOut = mysql_store_result(Conn)

   If MySQLOut = 0 then Print "Error"
    
   TotalCol = mysql_num_fields(MySQLOut)
   TotalRow = mysql_num_rows(MySQLOut)
    
	If debugmode = 1 Then print TotalRow
	
	
	Select Case TotalRow
		Case 0 ' Nothing found
	    	mysql_free_result(MySQLOut)
   	 	mysql_close(Conn)
			MessageToDisplay("Cashier_Error", "Cashier not found", "Sorry your user ID cound not", "be found Please check and", "try your number again","Press clear to carry on","","","")
			Exit Sub
	
		Case 1 ' Record Found
				   CashierNumber = *Row[0]
			 CashierNameDisplay = *Row[1]
					  KeyPadInput = ""
		    mysql_free_result(MySQLOut)
		    mysql_close(Conn)
			SignOnScreen_Password
			Exit Sub
	End Select

End Sub
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: FBTemp Crashing when trying to access Database

Post by Gablea »

Gablea wrote: Jul 25, 2022 10:59

Code: Select all

Aborting due to runtime error 7 (null pointer access) at line 58 of c:\algPoS\NPoS\database.bi::FINDCASHIER()
I sorted the above issue out but what is Error 12 "Segmentation Violation" mean?
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: FBTemp Crashing when trying to access Database

Post by caseih »

The error message is quite clear: You are dereferencing a pointer that is set to zero. The error clearly points to the offending line. I am unable to determine what Row actually is, since it's not defined nor assigned in the code you posted. So the problem is that either Row is null, or Row[0] is null. I think you've forgotten a step somewhere. Possibly a mysql_fetch_row() call. See https://dev.mysql.com/doc/c-api/5.7/en/ ... h-row.html
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: FBTemp Crashing when trying to access Database

Post by Gablea »

I sorted the above issue out but what is Error 12 "Segmentation Violation" mean?

I have no idea what this means could someone tell me what it means

below is the code from where it says the issue is

Code: Select all

	TotalCol = 0
   TotalRow = 0
		  CiR = 0
		  CiC = 0
    
	MyQuery = ""
	MyQuery += "select "
	MyQuery += "userid, "					'0
	MyQuery += "userpassword, "			'1
	MyQuery += "username, "					'2
	MyQuery += "usernameposrecipit, "	'3
	MyQuery += "changepassword, "			'4
	MyQuery += "useraccesslevel "			'5
	MyQuery += "from usertable "
	MyQuery += "WHERE "
	
	Conn = mysql_init(NULL)

   If (Conn = 0) then 
   	Print "Error"
   Else
   	Print "Conn ok"
   EndIf
   
   If mysql_real_connect(Conn, ServerIP, ServerUserName, ServerUserPassword, ServerDatabaseName, Val(ServerPortNumber), NULL, 0) = NULL Then
    	Print "Error Connecting to Database"
    	Print ServerIP
    	Print ServerUserName
    	Print ServerUserPassword
    	Print ServerDatabaseName
    	Print Val(ServerPortNumber)
   Else
   	Print "Connection Estabablished"
   EndIf

   If mysql_query(Conn, MyQuery) then Print "Error"

   MySQLOut = mysql_store_result(Conn)

   If MySQLOut = 0 then Print "Error"
    
   TotalCol = mysql_num_fields(MySQLOut)
   TotalRow = mysql_num_rows(MySQLOut)
    
	If debugmode = 1 Then print TotalRow
	
	
	Select Case TotalRow
		Case 0 ' Nothing found
	    	mysql_free_result(MySQLOut)
   	 	mysql_close(Conn)
			MessageToDisplay("Password_Error", "Password Error","Sorry the Password you have entered","does not match the one on","record. Please try again","Press clear to carry on","","","")
			Exit Sub
	
		Case 1 ' Record Found
			Row = mysql_fetch_row(MySQLOut)
			
				Select Case Trim(*Row[4]) ' Check to see if Password Needs to be changed
					Case "0", "N"	' Password Does not need to be changed

						KeyPadInput_Password = ""
									KeyPadInput = ""
			    		 	 CashierNamePrint = Trim(*Row[2])
					 	 CashierSignOnAccess = Trim(*Row[5])
				  	  CashierOverrideAccess = 0
					 	 ErrorAccessFunction = ""
					       	 CashierAccess = CashierSignOnAccess	'Set the system to use the signed on cashier 
					  	  PriceOverridePrice = 0               		'Clears the Entered Price 
					         	   TotalDue = 0.00 						'Resets sale value
					    		 	  ItemsSold = 0 							'Resets item count
					   	 	 TotalTendered = 0							'Resert the total tendered value
					   	     	  ChangeDue = 0							'Reset change due values as well
					 		  	  RecipitClear = "Yes"						'Set the recipit so a new one can be produced for sale
					 		      	SaleMode = "Sale"						'Sets the salemode back to sale
				'		 	 RecallInProgress = "No"						'Resets the Recall trigger so the system will run in normal mode
					 	     	  MSSavedValue = 0
					 	 	 ShowTaskBarItems = 1
					 	  	  SubTotalPressed = 0
					 	        RecPrintData = ""
				 	  	  MenuBeingDisplayed = "SaleMenu" 
				      	     DisplayLine1 = ""
							     DisplayLine2 = ""		
								   KeyPadInput = ""
					    mysql_free_result(MySQLOut)
					    mysql_close(Conn)
						SaleScreen
				
					Case "1", "Y"	' Password needs changing
			  			Row = mysql_fetch_row(MySQLOut)
					   mysql_free_result(MySQLOut)
				    	mysql_close(Conn)
				    	
								   KeyPadInput = ""
			  			'PasswordChange1
			    	
				End Select
			Exit Sub
	End Select
I really do appreciate the guidance with this



OH MY GOD I AM SUPER thick. I did not noticed I had not finished the Where clause much appreciated with the help I can now really debug the app more now thanks to this now method (could this be a one of the reasons why the app would crash if I was not catching the issues correctly)
fxm
Moderator
Posts: 12083
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FBTemp Crashing when trying to access Database

Post by fxm »

Gablea wrote: Jul 25, 2022 15:23 I sorted the above issue out but what is Error 12 "Segmentation Violation" mean?

I have no idea what this means could someone tell me what it means
"segmentation violation" or "segmentation fault":
the system has detected that the process was attempting to access a memory address that does not belong to it.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: FBTemp Crashing when trying to access Database

Post by Gablea »

fxm wrote: Jul 25, 2022 15:40
Gablea wrote: Jul 25, 2022 15:23 I sorted the above issue out but what is Error 12 "Segmentation Violation" mean?

I have no idea what this means could someone tell me what it means
"segmentation violation" or "segmentation fault":
the system has detected that the process was attempting to access a memory address that does not belong to it.
Im not suprised as I did not finish the WHERE statement so i bet the mysql server had no idea what to do with the request so it send nothing back
Post Reply