EXE Crash when call sub with Button Event

New to FreeBASIC? Post your questions here.
Post Reply
Tonigau
Posts: 36
Joined: Feb 25, 2021 20:19

EXE Crash when call sub with Button Event

Post by Tonigau »

I have some code working with WinGui.bi, but if I have any code in my 'Sub BuildPtrTabl' (called by Build Button) other than just to set Editor to a string value it crashes.

First paste some multiline text into top editor, then Build button to see lower editor result.
I have block commented the code & can see working, if un-comment block it will crash.
At some point I was able to debug (in FBedit) & see the code working.
Using Win7Px64

The sub code extracts lines from string retrieved from Editor1 & rebuilds the string for set Editor2 to new string.

Hope someone can help...

Code: Select all

'-------------------------------------------------
' StringTableconv.bas    V1.0
' Tool for String Table --> String pointer Table
' Created on 2021-03-17  ToniG
' Last Update 2021- - 
'-------------------------------------------------

#Include "WinGUI.bi"

Dim As HWND Window_Main, Editor1, Editor2, Btn_Build, Btn_Copy, Btn_Paste, Btn_Clear, Edit_Font, Btn_Exit
Dim As HFONT hFont
Dim As MSG msg
Dim As Long x0, y0, W1, H1, Rpos
Dim As String Text1
Declare Sub BuildPtrTabl (Editor1 As HWND, Editor2 As HWND) 

  W1 = 1000	' Window Width
  H1 = 700  ' Window Height
  Rpos = 200 ' Right ref
  Text1 = "Input:  String Table          Paste ClipBoard   (xxxx)"
           														' " _" line continuation wont work!!
'Create a window with text editors and a button:
'                            X    Y   W   H
Window_Main = Window_New	(100, 100, W1, H1, "Create String Pointer Table")
                                     ' "'    Paste Table here..." + Chr(13) + Chr(10) Just for ref. editBox
Editor1 = Editor_New		(20, 40, 800, 350, "",, Window_Main)
Editor2 = Editor_New		(20, 410, 800, 200, "",, Window_Main)

Var Label_txt1 = Label_New	(30, 20, 750, 20, Text1,, Window_Main)
Var Label_txt2 = Label_New	(30, 390, 150, 20, "Output:  Pointer Table",, Window_Main)

Btn_Paste = Button_New		(W1-180, 100, 100, 40, "Paste",, Window_Main)
Btn_Clear = Button_New		(W1-180, 100, 100, 40, "Clear",, Window_Main)
Btn_Build = Button_New		(W1-180, 100, 100, 40, "Build",, Window_Main)
Btn_Copy = Button_New		(W1-180, 100, 100, 40, "Copy",, Window_Main)
Btn_Exit = Button_New		(W1-180, 100, 100, 40, "EXIT",, Window_Main)
'												^ Window resize event overides on form open

' Fonts
'hFont = Control_Createfont("Courier New",,, FW_THIN) 
hFont = Control_Createfont("Courier New",,, FW_BOLD) 
Control_SetFont(Editor1), hFont
Control_SetFont(Editor2), hFont



'--------------------------
Sub BuildPtrTabl (Editor1 As HWND, Editor2 As HWND)
'Sub BuildPtrTabl ()
	
' Just Testing for now...
' Paste some multi line text from clip board with Paste Button
' All works ok Clear Button, Copy button, editors.
' But if uncommentblock in this sub it crashes with Build button ??


    Dim As String TempDS, TmpStr, CRLF, InputString, OutputString, CurrLine, TempString 
    Dim As Integer  ChrPos,  StrStrt, LineStrt, CRLFpos, Temp1 
    Dim As Integer ClosedQuoteCounter, ClosedQuote, EscapeChar
	 'CRLF = Chr(13) & Chr(10) CRLF = Chr(13, 10) ' < Neither work !!!
	 
	InputString = EditBox_GetText(Editor1)
	
	EditBox_SetText(Editor2,InputString) '< For DEBUG
'Above line not even execute with unComment block below !!
	
/'  
	LineStrt = -1: TempString = ""
	Temp1 = Len(InputString) '< For DEBUG
	If Len(InputString) < 2 Then Exit Sub
'   SetFocus(Editor2)
 EditBox_SetText(Editor2,InputString) '< For DEBUG  
 'EditBox_SetText(Editor2,"") ' Clear first
	
	'Extract lines from InputString
Do While CRLFpos < Len(InputString)
	CRLFpos = InStr(LineStrt + 2, InputString, Chr(13, 10))
	CurrLine = Mid(InputString, LineStrt + 2, CRLFpos - (LineStrt -1)) 
	LineStrt = CRLFpos ' < Easier to Debug
	TempString &= CurrLine & "---" & Chr(13, 10)
	
Loop
		EditBox_SetText(Editor2,TempString)

'/

End Sub


'-------------------------------

'Event loop:
Do
	WaitEvent(Window_Main, msg)
	Select Case msg.hwnd
		Case Btn_Paste
			If msg.message = WM_LBUTTONUP Then
			  EditBox_Paste(Editor1)
			End If

		Case Btn_Clear
			If msg.message = WM_LBUTTONUP Then
'			  EditBox_Clear(Editor1) '< This only clears selected text
			  EditBox_SetText(Editor1,"")
			End If  		

		Case Btn_Build
			If msg.message = WM_LBUTTONUP Then
		      BuildPtrTabl(Editor1, Editor2)
'			  BuildPtrTabl(a,b)
			End If

		Case Btn_Exit
			If msg.message = WM_LBUTTONUP Then
'				Hmmmm... HOW TO Close this App ?				


			End If			

		Case Btn_Copy		
         If msg.message = WM_LBUTTONUP Then
          '  SetWindowText(Editor2, "Copy sets a little text, so that we have something to select")
            SetFocus(Editor2)
            SendMessage(Editor2, EM_SETSEL, 0, -1)
            EditBox_Copy(Editor2)


'				How to test if some "is selected" first ??
'          SendMessage(Editor2, EM_GETSEL, , )			  
			End If


	  Case Else	
		 'Resize Controls x if window size change
       Window_GetSize(Window_Main, x0, y0, W1, H1)
       Control_Resize(Editor1, 20, 40, W1-Rpos, 350)
       Control_Resize(Editor2, 20, 410, W1-Rpos, 200)
       Control_Resize(Btn_Paste, W1-(Rpos-40), 40, 100, 40)
       Control_Resize(Btn_Clear, W1-(Rpos-40), 100, 100, 40)
       Control_Resize(Btn_Build, W1-(Rpos-40), 200, 100, 40)
       Control_Resize(Btn_Copy, W1-(Rpos-40), 410, 100, 40)
       Control_Resize(Btn_Exit, W1-(Rpos-40), 600, 100, 40)			
	End Select
Loop Until Window_Event_Close(Window_Main, msg)

End

'Wait until window is closed:
Do
	WaitEvent(Window_Main, msg)
Loop Until Window_Event_Close(Window_Main, msg)

'Delete font:
	Control_DeleteFont(hFont)
End



badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: EXE Crash when call sub with Button Event

Post by badidea »

Here it does not crash, but the Do While CRLFpos < Len(InputString) ... Loop never ends.

For those wondering what WinGUI is, see: Simple WinAPI GUI library
Tonigau
Posts: 36
Joined: Feb 25, 2021 20:19

Re: EXE Crash when call sub with Button Event (WinGUI)

Post by Tonigau »

Thanks badiea, the loop should be... Do While CRLFpos <> 0 ' (preset CRLFpos to -1)

This was actually the root cause of the "crash".
What threw me off the scent was the GUI was not being updated from editor text assign code (maybe it was but didn't show as it couldn't get back to WinGUI to refresh window?) & the Debugger locked up as well. In endless loop it should still single step the lines!!

Now I just need to learn how to close the app with Exit button
Cheers

Code: Select all


#Include "WinGUI.bi"

Dim As HWND Window_Main, Editor1, Editor2, Btn_Build, Btn_Copy, Btn_Paste, Btn_Clear, Edit_Font, Btn_Exit
Dim As HFONT hFont
Dim As MSG msg
Dim As Long x0, y0, W1, H1, Rpos
Dim As String Text1
Declare Sub BuildPtrTabl (Editor1 As HWND, Editor2 As HWND) 

  W1 = 1000	' Window Width
  H1 = 700  ' Window Height
  Rpos = 200 ' Right ref
  Text1 = "Input:   Paste ClipBoard  Just A Test (xxxx)"
           														' " _" line continuation wont work!!
'Create a window with text editors and a button:
'                            X    Y   W   H
Window_Main = Window_New	(100, 100, W1, H1, "Win Gui Example Extract lines")
                                     ' "'    Paste Table here..." + Chr(13) + Chr(10) Just for ref. editBox
Editor1 = Editor_New		(20, 40, 800, 350, "",, Window_Main)
Editor2 = Editor_New		(20, 410, 800, 200, "",, Window_Main)

Var Label_txt1 = Label_New	(30, 20, 750, 20, Text1,, Window_Main)
Var Label_txt2 = Label_New	(30, 390, 150, 20, "Output:  Pointer Table",, Window_Main)

Btn_Paste = Button_New		(W1-180, 100, 100, 40, "Paste",, Window_Main)
Btn_Clear = Button_New		(W1-180, 100, 100, 40, "Clear",, Window_Main)
Btn_Build = Button_New		(W1-180, 100, 100, 40, "Build",, Window_Main)
Btn_Copy = Button_New		(W1-180, 100, 100, 40, "Copy",, Window_Main)
Btn_Exit = Button_New		(W1-180, 100, 100, 40, "EXIT",, Window_Main)
'												^ Window resize event overides on form open

' Fonts
'hFont = Control_Createfont("Courier New",,, FW_THIN) 
hFont = Control_Createfont("Courier New",,, FW_BOLD) 
Control_SetFont(Editor1), hFont
Control_SetFont(Editor2), hFont


'--------------------------
Sub BuildPtrTabl (Editor1 As HWND, Editor2 As HWND)
' Paste some multi line text from clip board with Paste Button

    Dim As String TempDS, TmpStr, CRLF, InputString, OutputString, CurrLine, TempString 
    Dim As Integer  ChrPos,  StrStrt, LineStrt, CRLFpos, Temp1 
    Dim As Integer ClosedQuoteCounter, ClosedQuote, EscapeChar
	 'CRLF = Chr(13) & Chr(10) 
	 CRLF = Chr(13, 10) 
	 
	InputString = EditBox_GetText(Editor1)
	
	LineStrt = -1: CRLFpos = -1: TempString = ""
	Temp1 = Len(InputString) '< For DEBUG
	If Len(InputString) < 2 Then Exit Sub ' < Dont bother with no data

	
	'Extract lines from InputString
Do While	CRLFpos <> 0
	CRLFpos = InStr(LineStrt + 2, InputString, CRLF)
	CurrLine = Mid(InputString, LineStrt + 2, CRLFpos - (LineStrt+2)) 
	LineStrt = CRLFpos ' < Easier to Debug
	TempString &= CurrLine & "---" & CRLF
	
Loop
		EditBox_SetText(Editor2,TempString)

' This is just an example(Rebuild the string & put in Editor 2)

End Sub


'-------------------------------

'Event loop:
Do
	WaitEvent(Window_Main, msg)
	Select Case msg.hwnd
		Case Btn_Paste
			If msg.message = WM_LBUTTONUP Then
			  EditBox_Paste(Editor1)
			End If

		Case Btn_Clear
			If msg.message = WM_LBUTTONUP Then
'			  EditBox_Clear(Editor1) '< This only clears selected text
			  EditBox_SetText(Editor1,"")
			End If  		

		Case Btn_Build
			If msg.message = WM_LBUTTONUP Then
		      BuildPtrTabl(Editor1, Editor2)
'			  BuildPtrTabl(a,b)
			End If

		Case Btn_Exit
			If msg.message = WM_LBUTTONUP Then
'				Hmmmm... HOW TO Close this App ?				


			End If			

		Case Btn_Copy		
         If msg.message = WM_LBUTTONUP Then
          '  SetWindowText(Editor2, "Copy sets a little text, so that we have something to select")
            SetFocus(Editor2)
            SendMessage(Editor2, EM_SETSEL, 0, -1)
            EditBox_Copy(Editor2)


'				How to test if some "is selected" first ??
'          SendMessage(Editor2, EM_GETSEL, , )			  
			End If


	  Case Else	
		 'Resize Controls x if window size change
       Window_GetSize(Window_Main, x0, y0, W1, H1)
       Control_Resize(Editor1, 20, 40, W1-Rpos, 350)
       Control_Resize(Editor2, 20, 410, W1-Rpos, 200)
       Control_Resize(Btn_Paste, W1-(Rpos-40), 40, 100, 40)
       Control_Resize(Btn_Clear, W1-(Rpos-40), 100, 100, 40)
       Control_Resize(Btn_Build, W1-(Rpos-40), 200, 100, 40)
       Control_Resize(Btn_Copy, W1-(Rpos-40), 410, 100, 40)
       Control_Resize(Btn_Exit, W1-(Rpos-40), 600, 100, 40)			
	End Select
Loop Until Window_Event_Close(Window_Main, msg)

End

'Wait until window is closed:
Do
	WaitEvent(Window_Main, msg)
Loop Until Window_Event_Close(Window_Main, msg)

'Delete font:
	Control_DeleteFont(hFont)
End



badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: EXE Crash when call sub with Button Event (WinGUI)

Post by badidea »

Tonigau wrote:Now I just need to learn how to close the app with Exit button
I don't know. I never use the Windows API.
Without the WinGUI wrapper, it seems to be, according to this example:
PostMessage(hWnd,WM_CLOSE,0,0) and then a WM_DESTROY happens.
Tonigau
Posts: 36
Joined: Feb 25, 2021 20:19

Re: EXE Crash when call sub with Button Event

Post by Tonigau »

Thanks, that got me in the right direction...

Tried various PostMessage..., SendMessage... (Window_Main, WM_DESTROY,0,0) (Window_Main, WM_CLOSE,0,0)
These removed keyboard focus but not close.

I tried just this line DestroyWindow (Window_Main) & it closes window,& remove from application list in task manager WooHoo.
But the exe is still a running process.

After some research I added PostQuitMessage(0) but exe still not close.
Tonigau
Posts: 36
Joined: Feb 25, 2021 20:19

Re: EXE Crash when call sub with Button Event

Post by Tonigau »

Ok I found it... (I am surprised it didn't bite me)

Just put Exit Do in the Btn_Exit button handler, this exits the application.
(and Delete font to stop memory leak)
Post Reply