Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of String Space in Visual Basic 6

Tags:

string

memory

vb6

We are getting an error in a VB6 application that sends data back and forth over TCP sockets. We get a runtime error "out of string space". Has anyone seen this or have any thoughts on why this would happen? It seems like we are hitting some VB6 threshhold so any other thoughts would be helpful as well.

like image 213
leora Avatar asked Sep 18 '08 16:09

leora


People also ask

How do I add a space to a string in VBA?

The VBA SPACE function is listed under the text category of VBA functions. When you use it in a VBA code, it returns a string which is consisting of a specified number of spaces. In simple words, with space function, you can create spaces by supplying numbers.

What is out of string error?

The Out of string space/not enough storage is an error that Windows returns when Contributor is trying to allocate a string into memory and there is not enough contiguous memory to create the string.

What is string space?

String spacing is measured from the center of one string to the center of another string; this results in the actual spacing between the lower strings, which are thicker, being smaller than that between the higher strings, which are thinner.


2 Answers

As others have pointed out, every string concatenation in VB will allocate a new string and then copy the data over and then de-allocate the original once it can. In a loop this can cause issues.

To work around this you can create a simple StringBuilder class like this one:

Option Explicit

Private data As String
Private allocLen As Long
Private currentPos As Long

Public Function Text() As String
  Text = Left(data, currentPos)
End Function

Public Function Length() As Long
  Length = currentPos
End Function

Public Sub Add(s As String)

  Dim newLen As Long
  newLen = Len(s)
  If ((currentPos + newLen) > allocLen) Then
    data = data & Space((currentPos + newLen))
    allocLen = Len(data)
  End If

  Mid(data, currentPos + 1, newLen) = s
  currentPos = currentPos + newLen

End Sub

Private Sub Class_Initialize()
  data = Space(10240)
  allocLen = Len(data)
  currentPos = 1
End Sub

This class will minimize the number of string allocations by forcing the string to be built with spaces in it and then overwriting the spaces as needed. It re-allocates to roughly double its size when it finds that it does not have enough space pre-initialized. The Text method will return the portion of the string that is actually used.

like image 182
Jack Bolding Avatar answered Oct 12 '22 23:10

Jack Bolding


Text found on MSDN:

http://msdn.microsoft.com/en-us/library/aa264524(VS.60).aspx

Visual Basic for Applications Reference Out of string space (Error 14)

Specifics

Visual Basic permits you to use very large strings. However, the requirements of other programs and the way you manipulate your strings may cause this error. This error has the following causes and solutions:

  • Expressions requiring that temporary strings be created for evaluation may cause this error. For example, the following code causes an Out of string space error on some operating systems:
MyString = "Hello"
For Count = 1 To 100
MyString = MyString & MyString
Next Count
  Assign the string to a variable of another name.
* Your system may have run out of memory, which prevented a string from

being allocated.

Remove any unnecessary applications from memory to create more space.

For additional information, select the item in question and press F1.

like image 22
Robit Avatar answered Oct 13 '22 01:10

Robit