April 25, 2024, 12:36:11 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Faulty early exit from subroutine

Started by LurkinDerk, January 19, 2010, 11:38:30 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LurkinDerk

A dereferenced pointer in CBasic immediately following a NEW statement causes the sub to exit early and returns to the caller.
The following snippet is the failing code. The 2nd debug print statement never gets executed and the sub exits and immediatly returns to the caller (which happens to be a menu): Could someone please tell me why the sub is exiting early? The remainder of the program is syntax free.
/code/
SUB EnterIt (dummyparam:int)
' enter data into list
DIM info:    POINTER
DIM enterdone: INT
DIM lname$: STRING
DIM street$: STRING
DIM city$:    STRING
DIM state$: STRING
DIM zip$: STRING

'BEGIN-1
'**************
print "debug: inside Enter subroutine"
'**************
  enterdone = FALSE
  DO
    'get a new record
    info = NEW (Addr, 1)
    INPUT "Enter name: ", lname$
    IF LEN(lname$) = 0 THEN
      enterdone = TRUE
    ELSE
      'BEGIN-2
      'save name and enter other address info
      #info.name$ = lname$
'*************
PRINT "debug: #info.name$="; #info.name$
Waitkey ( )
'*************
      INPUT "Enter street: ", street$
      #info.street$ = street$
/end code/

ZeroDog

not sure if this is the problem, but the THEN statement is for single line IF statements.  Try removing the THEN from the IF statement and see if that helps at all.  Also, you do not have an ENDIF  for your IF statement, which is required for  IF/ELSE statements.

LurkinDerk

Thank you ZeroDog.
Removing the single like statement following the THEN helped, but changed the sub's behavior. Sorry that I did not include the entire sub to show it's completness. Following is the complete sub.  The 2nd print statement is never executed and the sub continues asking for input of name$. I still think the sub is exiting early because of the dereferenced pointer, but I cannot pinpoint just what the pointer reference is doing to it.

SUB EnterIt (dummyparam:int)
' enter data into list
DIM info: POINTER
DIM enterdone: INT
DIM lname$: STRING
DIM street$: STRING
DIM city$: STRING
DIM state$: STRING
DIM zip$: STRING

'BEGIN-1
'**************
print "debug: inside Enter subroutine"
'**************
enterdone = FALSE
DO
'get a new record
info = NEW (Addr, 1)
INPUT "Enter name: ", lname$
IF LEN(lname$) = 0 THEN enterdone = TRUE
ELSE
'BEGIN-2
'save name and enter other address info
#info.name$ = lname$
'*************
PRINT "debug: #info.name$="; #info.name$
Waitkey ( )
'*************
INPUT "Enter street: ", street$
#info.street$ = street$
INPUT "Enter city: ", city$
#info.city$ = city$
INPUT "Enter state: ", state$
#info.state$ = state$
INPUT "Enter zip: ", zip$
#info.zip$ = zip$
'store record
start = DSL_Store (info, start, last)
'****************
print "debug In Enter DO loop"
'****************
'END-2
ENDIF
UNTIL enterdone
'END-1 EnterIt
'***************
print "debug: leaving Enter subroutine"
'***************
RETURN
/code

Can you see anything else wrong? The Waitkey sub is defined elsewhere and does activate from other places in the program. The DSL_Store sub is also defined elsewhere. As I said in my earlier post the program compiles free of errors, but misbehaves at run time.
Thanks in advance. Rich.

ZeroDog

I dont think you can do an   IF / THEN / ELSE / ENDIF   in creativebasic.   

Its either   IF / THEN

Or   IF / ELSE / ENDIF

LarryMc

when pointers are used in a subroutine you have to tell them what the structure is.

From the help file section for the SETTYPE command:
TYPE MYUDT
    DEF age as INT
    DEF name as STRING
ENDTYPE

DEF pUdt as POINTER
pUdt = NEW(MYUDT,1)

'without SETTYPE using typecasting
#<MYUDT>pUdt.name = "Joe Smith"
#<MYUDT>pUdt.age = 7

'with SETTYPE
SETTYPE pUdt, MYUDT
#pUdt.name = "Joe Smith"
#pUdt.age = 7


Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library