Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Out of memory

Posted on 2000-05-02
Medium Priority
Last Modified: 2010-05-02
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.

Question by:gshriki
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3

Expert Comment

ID: 2770369
Visit to
"PRB: Repeated Load/Unload of Control Can Cause Out of Memory"

Expert Comment

ID: 2770378

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

LVL 14

Expert Comment

ID: 2770413
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>
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 28

Expert Comment

ID: 2770430
tell us about your family life...your likes and dislikes...hobbies...etc
LVL 12

Expert Comment

ID: 2770552
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.


Expert Comment

ID: 2770799
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.

Author Comment

ID: 2772109
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...
LVL 12

Expert Comment

ID: 2773093
The form is the key. Something is cascading. Check out the form_load and form_activate events.

LVL 14

Expert Comment

ID: 2773511
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.

Author Comment

ID: 2774111

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.
LVL 12

Expert Comment

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

LVL 14

Accepted Solution

wsh2 earned 600 total points
ID: 2774251
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>.

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question