[PB5.20b19] Recursion plus SELECT = Invalid Memory Access

Just starting out? Need help? Post your questions and find answers here.
Rick
User
User
Posts: 16
Joined: Thu Sep 23, 2004 6:25 am
Location: Portland, Oregon, USA

[PB5.20b19] Recursion plus SELECT = Invalid Memory Access

Post by Rick »

I am running PB5.20 beta 19 LTS (x64) on Mint 15 (Ubuntu variant).

If I use a SELECT...ENDSELECT statement with an embedded GOTO in a procedure that is recursive (calls itself), I get an error message after the first recursion. If I replace the SELECT...ENDSELECT with a IF...ELSEIF...ELSE...ENDIF structure with an embedded GOTO, it runs fine. The following code shows procedure 'recurse1' running fine, but procedure 'recurse2' will crash the program when the procedure tries to exit using either a Procedure Return or EndProcedure statement.

Code: Select all

EnableExplicit
Procedure.i recurse1(cnt.i)
	; create some variables unique to this instance of recursion
	Protected rc.i
	Protected x.i
	MessageRequester("Test","Recurse1 "+Str(cnt))
	If cnt = 0
		rc = cnt + 1 ; do some work
		x = recurse1(rc) ; recursively call myself
	Else 
		rc = cnt + 1 ; do some work 
		Goto AllDone1
	EndIf	
	x = x + 1 ; do some work
AllDone1:	
; This routine works properly
	ProcedureReturn rc
EndProcedure

Procedure.i recurse2(cnt.i)
	; create some variables unique to this instance of recursion
	Protected rc.i
	Protected x.i
	MessageRequester("Test","Recurse2 "+Str(cnt))
	Select cnt
		Case 0
		rc = cnt + 1 ; do some work
		x = recurse2(rc) ; recursively call myself
		Default
		rc = cnt + 1 ; do some work 
		Goto AllDone2 ; this GOTO causes the error when the procedure exits.
	EndSelect
	x = x + 1 ; do some work
AllDone2:	
; Error: INVALID MEMORY ACCESS HERE
	ProcedureReturn rc
EndProcedure

Define x.i
x = recurse1(0)
x = recurse2(0)

MessageRequester("Test","Done")

This example is not intended to discuss the merits of coding style, but is to illustrate that a valid set of basic instructions causes a memory access problem when a SELECT statement combined with a GOTO statement causes the program to crash.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: [PB5.20b19] Recursion plus SELECT = Invalid Memory Acces

Post by davido »

Would this comment by Idle assist you?

http://www.purebasic.fr/english/viewtop ... 33#p423633
DE AA EB
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: [PB5.20b19] Recursion plus SELECT = Invalid Memory Acces

Post by Little John »

Rick wrote:This example is not intended to discuss the merits of coding style, but is to illustrate that a valid set of basic instructions causes a memory access problem when a SELECT statement combined with a GOTO statement causes the program to crash.
This has been discussed here before, e.g. in December 2009.
Some info about this in the docs is needed.
Rick
User
User
Posts: 16
Joined: Thu Sep 23, 2004 6:25 am
Location: Portland, Oregon, USA

Re: [PB5.20b19] Recursion plus SELECT = Invalid Memory Acces

Post by Rick »

Thanks davido, yes that explains things...
Thanks to you also Little John, it shows how far back this problem goes. It it's understood what's happening, I wonder why the Syntax Check has not been updated in all these years to flag it as a no-no since it breaks the execution with a known stack misalignment?

Now I know better..., for today..., until I forget a year from now and have to research it again :)
Post Reply