Out of memory

Hey there.
i'm having a problem with error 7 - out of memory.
it happens when the user press a button that loads a form.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

wsh2Connect With a Mentor Commented:
Full Setups are the only way to go.. To bad we don't get a nickel everytime someone just copies the EXE code.. <smile> and a <wink>.
Visit to
"PRB: Repeated Load/Unload of Control Can Cause Out of Memory"
From http://support.microsoft.com/support/kb/articles/Q90/1/45.asp 

PRB: Repeated Load/Unload of Control Can Cause Out of Memory

The information in this article applies to:

Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0


If you repeatedly load and unload a dynamic control (a control that is a member of a control array), you may receive an "out of memory" error (error 7).

The problem is that Visual Basic is not able to release all memory used by the control until the current event structure has terminated. The current event structure could be an event procedure, a modal form or dialog box, or a general procedure.

To avoid this problem, do not repeatedly load and unload controls without first allowing the event structure to terminate.

This behavior is by design.

The example below demonstrates this limitation.

' To try this example in VBDOS.EXE:
' 1. From the File menu, choose New Project.
' 2. From the File menu, choose New Form.
' 3. Add a command button (Command1).
' 4. Set Command1.Index = 0.
' 5. Double-click on the form to return to VBDOS.EXE.
' 6. Copy the code example into the Form_Click event procedure.
' 7. Press F5 to run the program.

Sub Form_Click ()
   PRINT "Loading 50 Controls 100 times...Please be patient"
   FOR j = 1 to 100
      PRINT "Loop: " + STR$(j)
      x = DoEvents()
      FOR i = 1 to 50
          Load Command1(i)
      NEXT i
      FOR i = 1 to 50
          UnLoad Command1(i)
      NEXT i
   NEXT j




     PRINT "Loop Index: ";j
     x = DoEvents()
     RESUME leave

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tell us more about your application.. how many forms are already loaded.. what else is running in the machine.. are you using highly detailed graphics (the picture file could be corrupted).. what version of VB, and is your VB current with Service Pack 3.. what controls are on the form.. are you using APIs.. those kind of thingz.. <smile>
tell us about your family life...your likes and dislikes...hobbies...etc
Typically this problem shows when you have an event cascade. This occurs
when your app recursively calls a function. For example if you code:

Text1.Text = Text1.Text & " "

and you place that line in the Text1_Change() routine you'll trigger an
event cascade as soon as you make any change in the text box. This is
because the first change triggers the concatenation that triggers the
change that triggers the concatenation that triggers the change that
triggers the ....

Look at what you're doing the the Click event. Something there is
causing a cascade. Try remming out any subroutine calls to isolate the
trouble code. These are sometimes insidious to find if the cause is
indirect. That is your click event calls a routine that calls a routine
that calls the click event. These can be tough to find, but if you put a
debug.print in the click event (or the child subs) you should be able to
spot what is cascading.

How to stop runaway recursion in event handlers.  I've had to do this sometimes when I want to update the contents of control (such as a textbox), while still responding to events caused by user input:

sub Text1_Change
Static Event_Pending as boolean
If not event_pending then
   event_pending = true
   ' DO my event handling code like ...
   text1.text = text1.text & " "
   ' all done with it....
   event_pending = false
end sub

"Static" keeps the memory for the "Event_Pending" flag allocated, and most importantly, uninitialized from one Change event to the next.
gshrikiAuthor Commented:
Many thanks for the comments. i'll tel you more about the application :
it's very simple application with 5 forms. the first one has 4 buttons on it, and each one calls onother form.
the error occures every time i try to enter a specipic form, and to the other forms i can enter smoothly.
i'm using VB5, and the form that causes the problem has a TAB control on it.
when i open the form, i'm running the GetVersion API (but i don't think the problem is there because another application uses this API and work just fine).
mark and jet - there is no cascade events on the form.
ruchi - thanks but i already searched in the technet.
azrasound - maybe later...
The form is the key. Something is cascading. Check out the form_load and form_activate events.

I tend to agree with Mark2150.. as your application is fairly simple.. please copy and paste the failing form's code here.. and the code from where it is being called.
gshrikiAuthor Commented:

the problem is solved. after i read wsh2 note, i checked out the controls, and find out that the tab control in the client PC is very old (he did not installed the program by setup),
so i copied the tab control OCX to his windows system directory and it works just fine.
thanks to all.
wsh2, post an answer to collect the points.

Thanks again.
No need for wsh2 to post for points. Just use the Accept Comment as Answer button over one of his responses...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.