Out of memory

Posted on 2000-05-02
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
  • 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>
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.
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.


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 200 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now