Out of String Space in Visual Basic 6

后端 未结 6 1473
误落风尘
误落风尘 2020-12-16 18:03

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

相关标签:
6条回答
  • 2020-12-16 18:20

    It sounds like you are appending a string often. You could try using a StringBuilder class

    Also, it could be you have some stale objects that contain strings hanging around that aren't being used and should be freed. Check for circular references perhaps by logging object allocation/frees in Class_Initialize/Class_Finalize

    0 讨论(0)
  • 2020-12-16 18:22

    Adding to Jacco's response, vbAccelerator has a great String Builder class that accomplishes much the same thing but is a little more robust. The author also walks through the solution explaining how it works.

    0 讨论(0)
  • 2020-12-16 18:22

    Assuming that you are appending data in a loop, ensure that it's not being appended to itself, which will eat memory extremely quickly.

    Example and description of error meaning: http://msdn.microsoft.com/en-us/library/aa264524.aspx

    0 讨论(0)
  • 2020-12-16 18:24

    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.

    0 讨论(0)
  • 2020-12-16 18:33

    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.

    0 讨论(0)
  • 2020-12-16 18:34

    Sometime in the spring of 2009, Microsoft did an XP update that interferes with Armadillo/Silicon Realms wrapper. The line of code that was throwing error 14, Out of String space was not logical. There was no problem with a over sized string. It was a simple assignment that I even changed to be "foo" and error 14 still occurred. I think the error is mapped incorrectly in XP. The answer for us was to remove copyMem-11 from the Armadillo protection project and rewrap the the exe.

    0 讨论(0)
提交回复
热议问题