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>
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

861 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