Stack Fault and ValueS() Question

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
Don_Auld
Posts: 3
Joined: 27 June 2007, 14:10 PM
Location: Vancouver Canada

Stack Fault and ValueS() Question

Post by Don_Auld »

I'm experiencing a stack fault problem when I pass a Mid() function as parameter 1 to a ValueS() subroutine. The following code, while nonsensical, reliably reproduces the problem.

Code: Select all

Option Explicit

Sub Main()
	Dim S As String
	Dim Sng As Single
	Dim Success As Boolean
	Dim I As Integer
	
	'Enable Stack Checking
	Call StackCheck(True)
	
	'Say Hello
	Console.Writeline ("Program Start: ")

	'Reset Flags: 2=HW Reset, 4 Brown out, 5=Power, 8=Watchdog, 16 JTAG reset
	Select Case Register.ResetFlags
		Case 2
			Console.Writeline (" [Reset Button]") 
		Case 5
			Console.Writeline (" [Power]")
		Case 8
			Console.Writeline (" [Watchdog Timeout]") 
			Console.WriteLine ("Fault Type: " & CStr(Register.FaultType))
			Console.WriteLine ("Task Control Block: " & CStr(Register.FaultData))
			Console.WriteLine ("Instruction Address: " & CStr(Register.FaultData2))
			Console.Writeline ("Reset cause MCUSR     : &H" & cstrhex(Register.MCUCSR) )
	End Select
			
	Console.WriteLine ("Register.ResetFlags: &H" & CStrHex(Register.ResetFlags))
		
	S = "0A1B2C3D4"
	Sng = 0.0
	Success = False
	
	Do
		For I = 1 To Len(S)
			Call ValueS(Mid(S,I,1),Sng,Success) 'THE PROBLEM!
		Next I
	Loop
	
End Sub

My Environment:
ZX24a v2.6.2
Zbasic IDE v1.4.5
Compiler version: v2.6.4

I solved my problem by passing a string to the ValueS() function.

Code: Select all

	Do
		For I = 1 To Len(S)
			NewStr = Mid(S,I,1)
			Call ValueS(NewStr,Sng,Success)
		Next I
	Loop

I'm inexperienced with ZBasic and I'm not sure if i am using the ValueS() function incorrectly or if there is a problem in ZBasic.

Also, In my first example, if I comment out either the Do/Loop or the For/Next loop the problem disappears.

Don
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Stack Fault and ValueS() Question

Post by dkinzer »

Don_Auld wrote:I'm experiencing a stack fault problem when I pass a Mid() function as parameter 1 to a ValueS() subroutine. [...]I'm inexperienced with ZBasic and I'm not sure if i am using the ValueS() function incorrectly or if there is a problem in ZBasic.
There is no problem with the way that you've used ValueS(). We have confirmed the problem and verified that it only occurs in VM mode devices (such as the ZX-24a).

The issue is that the string returned by Mid() is not being freed in the code that implements ValueS(). Consequently, when you do this inside an infinite loop, the heap grows until it collides with the stack, causing the stack fault that you reported.

As you've noted, saving the string returned from Mid() to a temporary variable and then passing that variable to ValueS() avoids the problem and is therefore a good workaround.

We will post a VM update when we've completed the testing.
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Stack Fault and ValueS() Question

Post by dkinzer »

dkinzer wrote:We will post a VM update when we've completed the testing.
The VM update that corrects this problem is available on the Downloads Page.
- Don Kinzer
Post Reply