Managing objects in VBA ( specifically good practices with destruction)

  • Thread starter Thread starter Tom Med
  • Start date Start date
T

Tom Med

I am having a lot of problems with VBA instability at the moment. And
I am pretty sure this is to do with the destruction of objects and
their deletion from memory. I get this instability everytime I try to
stop the VBA or to close certain workbooks. Does anyone have any
information or resources about how VBA manages it objects? Or does
anyone know what known problems there are with certain types of object
that could cause instabilities? Sort of like the C# problem with
structs vs classes. Are there certain things I should to to ensure
the clean removal of objects within classes?


Thanks
Tom
 
when object var goes out of the scope [fe after returning from the sub that
has created it] does it also mean that referenced object is properly removed
from memory [assuming that was the last refernece to that object]?
 
Look at this thread of 26 Sept 2007:
VBA6 cores when hitting Reset button on the VBA IDE

RBS
 
Yup I have already done that, I have also cleared down any member
variables of classes in the terminate method. I am looking for
something that will explain how vba manages objects, or will show me
the internals of what vba is doing with all my objects
 
Sure. When I try and close a certain workbook and hence VBAProject I
get a coredump. When I try to stop the VBE execution in this project
I get a core dump. When I try and compile this project I get a core
dump. Due to the nature of these problems it is hard to narrow it
down from a project level. Although I am trying to run the modules in
this project separately to see if I can locate the exact cause.

Thanks
 
And I have now discovered it crashes when I try to remove a module or
class.... so basically it is impossible to debug too.

I have updated to the latest vbe6.dll I could find on the MS website
and done everything else I know how to do and am totally out of ideas
now
 
Hi Tom,

You have posted a few questions about this instability. No-one reading can
really provide a comprehensive answer since we've got no understanding of
what you are doing. People can provide a few pointers but it's very hard to
help when you cannot see the code.

My "code unseen" tip - create a new workbook and cut and paste all of your
VBA into there then re-compile.
 
Would that make a difference?
I use the commercial Workbook Rebuilder and I think that does make
a new wb, but not sure.

This thread reminds me of a thread some years ago, started by a Belgium, who
had a large commercial .xla that was messed up in a similar way.
Eventually we got it working again.

I wonder if it wouldn't be better if Option Explicit was added by default
to every component, so you would need to do Don't require variable
declaration to not have this. It sure would avoid a lot of trouble.

RBS
 
Sorry for the delayed response, I had to demo something. I have moved
all my code across to an old stable version of the sheet and put in
'explicit' everywhere. However I am still getting a core in VBA when
I try to compile ( in the proces of finding all the compile errors
from "Option Explicit". At the moment I am trying recreate this on
another sheet that was created from blank, but I getting errors trying
to reference my other add ins I need to compile the code.

It is hard to narrow this down to a code extract I can give John as
all I get is a fatal exception
 
Just post the wb and I will have a look.
Obviously no guarantee I can make it work.
Should the wb work by itself or are there any external
libraries other than the standard VBA ones?

RBS
 
I am trying to create a version that is portable ( i.e. emailable)
but still exposes the bug
 
I have had partial success manually removing all the VBA errors due to
"Option Explicit" by exporting to file then changing and re-
importing. It now does not crash when it closes or compiles, it
sometimes crashes when you stop debuging too, but I cannot reproduce
this reliably yet.

When I can reproduce another reliable crash I will post a
selfcontained example

Thanks very much for your help, I feel like I have half broken its
back with the Option Explicit, that was a good suggestion

Thanks
Tom
 
Looks you are making progress.
Just remember to force Option Explicit from now on as explained.

RBS
 
If I encountered something like this, I would suspect some kind of
corruption. Sure, it's a lousy excuse for not knowing what's wrong, but
whatever.

I would copy the code from each module into my text editor and save a pile
of text files. I would rebuild the problematic project bit by bit to make
sure it's not a simple coding error (though I usually make complicated
errors <g>). I'd go through and compile often to try to see where the
problem creeps in.

Other than this kind of brute force rebuilding of the project, I'm not sure
what to suggest.

- Jon
 
Except the OP claims that removing a module crashes Excel. The code cleaner
sequentially removes all the modules, saves the project, then adds the
modules back in.

- Jon
 

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

Back
Top