Can't free the memory after unloading the userform

C

cyberdude

Hi,

I found the problem that Word can't free the memory after a userform
is unloaded. An example is given here: I opened a Word session and,
after that, I found from Taskmanager that the session used up 17280K
memory. I invoked an useform and then did nothing. The memory usage
jumped to 24320K. I clicked on the "unload" button containg the
"unload me" command and the memory usage was about 24400K. It showed
that the allocated memory couldn't be freed. Could someone help me by
solving this memory allocation problem?

Mike
 
D

Doug Robbins - Word MVP

How did you get the userform to show?

Try

Dim myform As YourFormName
Set myform = New YourFormName
myform.Show
Unload myform
Set myform = Nothing

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
C

cyberdude

How did you get the userform to show?

Try

Dim myform As YourFormName
Set myform = New YourFormName
myform.Show
Unload myform
Set myform = Nothing

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP








- Åã¥Ü³Q¤Þ¥Î¤å¦r -

Hi,

I got the usrform to show by putting the following command in a macro,
say "activateform1", under the module in a project:

userform1.show false

May I ask where I should put your suggested commands? Should I put
them in a macro or a subroutine in a userform? Can you give me an
example? Thank you.

Mike
 
D

Doug Robbins - Word MVP

You should use those commands in place of the one that you have.

If this is more than an academic question, you might be interested in the
following rant from one Malcolm Smith

Quote
By magic form I mean the practice of intantiating an object
implicitly.

So example, say you create a form class called frmLetter then the
wrong way to instantiate the form would be:

frmLetter.Show

This is because frmLetter is a CLASS and not an OBJECT. What happens
is that a default object is created. There are a load of issues with
this method.

The first is that the scope of such an implicit object is global.
Now, one of the whole point about writing proper code is that objects
and variables have their own scopes - all designed to be as tight as
possible.

If you go and bust the whole scope open then (a) it's bad programming
practice and (b) people can actually access the form from anywhere
within the code. Also, it goes to show other developers that the
programmer has a problem distinguishing between classes and objects.
Not good for one's reputation.

The other problem; one that I have seen time and time again is that
quite often the implicit object is not destroyed in memory if the same
template is run again within the same session. For example, the last
time I was called out to solve this problem was to a London Law Firm
who were complaining that the users were calling up a letter or fax
template and that the information entered in the fields last time were
there for the next letter.

The problem was with Magic Forms and the way that the programmer
called them. The answer is to create an object explicitly:

Dim oForm as frmLetter

Set oForm = New frmLetter
oForm.txtDate.Text = Format$(Date(), "d mmmm yyyy")
oForm.Show vbModal
' ----------------------------------
Unload oForm
Set oForm = Nothing


Then everything is nicely wrapped up at the end and there is nothing
horrible hanging about in memory afterwards.

It is not a co-incidence that in .Net magic forms are no longer
allowed. One has to create an explicit object. The general response
from the VB community was "finally they have fixed this".

Magic forms are one of the dreadful shortcuts and defaults permitted
(nay, encouraged) by Microsoft and they never, ever, should be used.
There are a number of these diseased defaults in Word and over the
course of the New Year I will itemise these on my web site.

But, please believe me that using magic forms (i.e. implicitly calling
the Class) is not good programming at all. I have not met anyone who
who disagrees with me and can put forward a valid reason. All of the
serious VB and VBA developers that I know and respect all say the same
thing "magic forms are a spawn of the devil and should be avoided".

Until someone can give me a good strong reason why magic forms are a
good idea this will be my stance. And, believe me, I and others
better than I have thought long and hard about this.


As an example of another diseased possibility in VB/VBA. Why on
earth is Option Explicit by default turned off?

Anyway, the only reason why I won't do the site now is that tomorrow
it's my birthday and I think that I deserve going out to celebrate
another year gone...



Unquote

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

How did you get the userform to show?

Try

Dim myform As YourFormName
Set myform = New YourFormName
myform.Show
Unload myform
Set myform = Nothing

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP








- Åã¥Ü³Q¤Þ¥Î¤å¦r -

Hi,

I got the usrform to show by putting the following command in a macro,
say "activateform1", under the module in a project:

userform1.show false

May I ask where I should put your suggested commands? Should I put
them in a macro or a subroutine in a userform? Can you give me an
example? Thank you.

Mike
 
G

Gordon Bentley-Mix

Doug/Mike,

It appears that there may still be a small memory leak. I don't use "magic
forms" but I note that memory usage still goes up by a small amount each time
the form is loaded and then unloaded again - on average 216K over 5 runs.
It's not a huge amount, though, and certainly nothing I'm going to get
terribly concerned about, and it does appear to stablise after a while. In
addition, simply opening and closing a blank document seems to clean
everything up again, and when I do this, I note that the "scratch file"
associated with the UserForm-containing template hangs around until another
document is opened and closed again; perhaps this is the source of the leak?

Interestingly enough, the same behaviour appears to occur even when opening
and closing a series of blank documents, so I highly doubt that the leak is
in any way related to loading / unloading a UserForm...
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top