Too many local, nonstatic variables error in VB 4.0/5.0
From Support
| | If you have a question or seek clarification, please call Technical Support. |
Contents |
Problem:
User gets a "Too Many local, nonstatic variables" error from the subroutine CDDAGETLASTCPF during VB 4.0 or 5.0 program compilation.
There seems to be a bug in VB 4.0 & 5.0 where a Globally Type Declared variable allocates stack space for each usage of the variable within a function. In this case, the ddaGetLastCPF function was being called 21 times within the CDDAGETLASTCPF function, thus allocating the entire LastCPF message variable (1578 bytes) 21 times = 34,716 Bytes of memory being allocated.This would overflow the local variable space which can have a maximum allocation by VB of 32,000 bytes.
Solution:
The following described code workaround eliminates the problem seen in the CDDAGETLASTCPF routine. Evidently when a standard string variable is referenced in each ddaGetLastCPF call, the system doesn't allocate local string space like it does with Type Declared variables, thus eliminating the problem.
Step 1: Change Declaration
Change the ddaGetLastCPF Declaration in a CoreCode Generated VB module as follows:
Declare Function ddaGetLastCPF Lib "dda40032.dll" (ByVal session As Long, ByVal LastCPF As String) As Long
Step 2: Create Variable
Create the following variable in the Global Variables section of the DDA/00 created BAS Module:
Global LastCPF2 As String * 1579 'Temporary Last CPF Message String
Step 3: Change References / Add Code
- Change all LastCPF references to LastCPF2 for each of the ddaGetLastCPF function calls in CDDAGETLASTCPF
- Add the Mid$ block of code at the end of the CDDAGETLASTCPF function so the Messages are parsed correctly into the LastCPF structure from string variable LastCPF2
EXAMPLE CDDALASTCPF Function: Function CDDAGETLASTCPF(ByVal EFormat As String) As Integer '************************************************************ '** ** '** Function: CDDAGETLASTCPF ** '** Purpose: Get the last CPF message (if any) ** '** Parms: None. ** '** Returns: DDASUCCESS: LastCPF holds last CPF message. ** '** Other: Error Occurred. Check Return Code** '** ** '************************************************************ If UCase$(EFormat) = "ITEMMAST" Then 'Get the last CPF message... ddaret = ddaGetLastCPF(HOST_ARRAY(1).HostSession, LastCPF2) End If '************************************************************ 'Before Exiting this routine we need to fill in the LastCPF 'Message Structure by parsing variable LastCPF2 '************************************************************ LastCPF.MsgID = Mid$(LastCPF2, 1, 7) LastCPF.MsgType = Mid$(LastCPF2, 8, 15) LastCPF.MsgFile = Mid$(LastCPF2, 23, 20) LastCPF.ExtraInfo = Mid$(LastCPF2, 43, 512) LastCPF.MsgData = Mid$(LastCPF2, 555, 512) LastCPF.MsgText = Mid$(LastCPF2, 1067, 512) CDDAGETLASTCPF = ddaret End Function
