Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TEXT...ENDTEXT updates a hidden variable instead of creating private variable 3

Status
Not open for further replies.

luchini

Programmer
Aug 25, 2017
2
US
Ran into this strange bug today. Seems to be a lesson in how VFP scoping can be weird, and why you should always initialize variables. Code is below.

Code:
CLEAR

*
* [1] Initialize pcVar, which is PRIVATE by default
*
pcVar = PROGRAM()
DISPLAY MEMORY LIKE pcVar

TestProc()

*
* [5] pcVar updated to 'TESTPROC'
*
DISPLAY MEMORY LIKE pcVar

RETURN

******************
PROCEDURE TestProc
******************
	*
	* [2] Hide pcVar from this procedure
	*
	PRIVATE pcVar
	DISPLAY MEMORY LIKE pcVar
	
	*
	* [3] Write to pcVar, which *should* be a new variable, private *to this procedure* by default
	*
	TEXT TO pcVar NOSHOW TEXTMERGE
		<<PROGRAM()>>
	ENDTEXT
	
	*
	* [4] TEXT...ENDTEXT just updated a hidden variable
	* Note: if pcVar were PUBLIC in the calling procedure, it would still get updated
	* 
	* Initializing pcVar after the PRIVATE command, but before the TEXT...ENDTEXT block would completely avoid this error
	*
	DISPLAY MEMORY LIKE pcVar
	
	RETURN
ENDPROC
*******

Output, annotated:

Code:
[1] PCVAR             Priv      C   "00002Q6H01J7"  00002q6h01j7
[2] PCVAR             (hid)     C   "00002Q6H01J7"  00002q6h01j7
[4] PCVAR             (hid)     C   "               TESTPROC"  00002q6h01j7
[5] PCVAR             Priv      C   "               TESTPROC"  00002q6h01j7

Essentially, though I hide pcVar from TestProc() with PRIVATE, TEXT TO pcVar sets the hidden, out of scope pcVar instead of creating a new private variable scoped within TestProc(). Interestingly, if there are multiple hidden variables at the same time, TEXT TO will set the closest variable in the callstack.

This isn't specific to out-of-scope PRIVATE variables: TEXT..ENDTEXT will also update out-of-scope PUBLIC variables.

This wouldn't be an issue if I had instantiated pcVar within TestProc() between the PRIVATE and TEXT TO command, which serves as a lesson to always instantiate your variables.
 
I can confirm, this even is in the last VP9 SP2 hotfix and the workaround is initalizing the variable.

The promise of the TEXT...EMDTEXT help topic is:
VFP help said:
If the variable has not yet been declared, Visual FoxPro automatically creates it as a private variable.
This is not done.

To illustrate this better you may change the line [tt]PRIVATE pcVar[/tt] within the [tt]TextProc[/tt] procedure to [tt]PRIVATE ALL[/tt]

That will make clearer the [tt]PRIVATE[/tt] command is no variable declaration command like [tt]LOCAL[/tt]. That still doesn't explain it, the promise of the help topic is not kept.

Here's an example showing how ASTACKINFO behaves correctly, the bug really is with TEXT..ENDTEXT, not with private variable scoping in general:
Code:
CLEAR

*
* [1] Initialize paVar, which is PRIVATE by default
*
? "[1] paVar initialized:"
ASTACKINFO(paVar)
DISPLAY MEMORY LIKE paVar

TestProc()

*
* [4] paVar uncganged by 'TESTPROC'
*
? "[4] paVar unhidden, unchanged by Testproc and exactly as initialized in [1]:"
DISPLAY MEMORY LIKE paVar

RETURN

******************
PROCEDURE TestProc
******************
   *
   * [2] Hide paVar from this procedure
   *
   PRIVATE ALL
   ? "[2] Show the incoming paVar is hidden:"
   DISPLAY MEMORY LIKE paVar
   
   * Set paVar, which will be a new variable, private *to this procedure* by default
   ASTACKINFO(paVar)
   
   *
   * [3] Show ASTACKINFO behaves correctly
   *
   ? "[3] Show the recreated internal paVar within TestProc (this section has paVar twice, once hidden (hid), once Private):"
   DISPLAY MEMORY LIKE paVar
   
   RETURN
ENDPROC
*******

Thanks for the heads up.

Bye, Olaf.
 
Thanks for the confirmation and help, Olaf.

Glad to know the bug only occurs for [tt]TEXT..ENDTEXT[/tt], and doesn't appear to be an issue with other functions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top