VB6 & COM+: Set <object> = Nothing irrelevant?

Status
Not open for further replies.
So as a VB programmer I've had the maxim of "always set your objects to Nothing" beaten into me. But another programmer and I were talking at work, and the idea hit both of us: What about running VB components under COM+? Wouldn't COM+ know when an object reference went out of scope?

So I ran me some tests. I made two simple classes. Class1's Test function instantiates an object, then exits. Class2's Test function instantiates an object, sets it to Nothing on the next line, then exits. I compiled both of these classes into DLLs and registered them in different COM+ applications. (This is on Win2K Pro.)

To run the COM+ applications, I used an ASP client that instantiated Class1 and Class2, then used a For loop to call both classes' Test methods 100 times.

As I expected, two new instances of DLLHOST.EXE began running, one for each of Class1 and Class2. They were both taking up about the same amount of memory.

I ran my ASP page a couple more times, and both instances of DLLHOST used a bit more memory, but eventually hit a memory usage plateau. The two instances were still using almost exactly the same amount of memory.

Then I let the application sit, idle. Both instances of DLLHOST started to decrease in memory usage, about 4~8KB every minute or two. Again, both instances were very close to each other in memory usage, and seemed to be releasing memory at roughly the same rate.


In short:
The application where object references weren't being set to Nothing wasn't using more memory than the application where object references were set to Nothing.


So... is there something wrong with my (admittedly rough) testing procedures, or do I not need to clutter up VB code that will run under COM+ with innumerable Set <object> = Nothing calls?


Q
 
From a memory usage standpoint, there may not be a need to use the 'Set <obj> = Nothing'. Of course, you're trusting COM+ to correctly handle the scope issues. Sure, you should be able to trust it... but part of the reason that the whole "set the objects to nothing" was being beaten into VB programmers heads is because earlier versions of VB didn't always do a great job of recognizing that an object variable had gone out of scope. Hence the need, at least at the time, to force object pointers to nothing rather than relying on the scope rules.

However, even if you can trust that the falling out of scope will be handled correctly, use of the "set to nothing" statements can still have value -- to the maintainance of the code. By including them, you're making it explicit when an object is no longer needed for your processing, rather than leaving it just hanging around. Making it explicit might help out the next poor guy who has to change the code years from now (and that guy might be you!) But this is strictly an issue of style rather than one of necessity.

Personally I'd put 'em in, but then I tend to program in a state of paranoia. :)
 

russ-iha

Ars Praefectus
4,407
Subscriptor
In Visual Basic, when reference variables go out of scope and there are no more references to the variable then it is destroyed. It has always worked that way to my knowledge.

There are issues with circular references, etc... but you get that in any reference counting system.


There is no need to explicitly set any object to "nothing" unless you wish to free the memory or resources the object holds before the end of a procedure... for example, in a recursive setup, you may wish to free objects so they don't get held in memory for all the iterations of the recursive subroutine.
 

DaleDBeast

Wise, Aged Ars Veteran
172
Here are some situations where I have traced errors and crashes to LACK of a "Set Object = Nothing" statement:

1. When using ActiveX controls in IE and the User clicks the Close button in the upper right.
2. When Object references a form, and the project is somewhat complex, say with 70 forms or so.
3. When Object references an automation object such as Excel.
4. When Object references an out of process VB exe that you had passed a callback object into.
5. When Object references a form containing controls that are asynchronously doing something, such FTP transfers.

There are probably more, but eventually its just better to terminate your object references in a deterministic manner. Also, on the larger VB projects, the ones with a complex object model that could be instantiating object trees in loops, it seems to me it would be asking for trouble if the teardown wasn't strictly controlled, or "deterministically finalized", as they say.
 

gregatron5

Ars Legatus Legionis
13,005
Subscriptor++
We have a fairly big, ok, pretty damn big VB application that had a huge memory leak. The lead programmer took 2 days to scour the code and actually found huge numbers of small ones. They were so bad that our program would crash after opening 2 or 3 projects. The simple act of finding as many places where 'Set <object> = Nothing' was left out and putting it in practically elimanted our memrory leaks. As mentioned, there are several documented problems with VB releasing memory, and the general feeling, at least here, is that it should not be trustad at all.
 
Thanks for the input. Now, some more questions. :)

Here are some situations where I have traced errors and crashes to LACK of a "Set Object = Nothing" statement:

That's a handly list; thanks. Does anyone know of some official or semi-official documentation on known memory leaks?

One other thing:
What's the difference between running a DLL in a COM+ application vs. out of one? Specifically, does the COM+ object reference counter replace the (apparently buggy) VB reference counter, or does the COM+ counter simply wrap around VB's broken counter?


TIA!
 

TheJet

Ars Praefectus
4,598
Subscriptor++
The short story is that COM+ provides the surrogate host for your DLL, rather than your DLL being located in the same process space as your application. This implies a number of things (in the default setup, things get weirder if you choose to create a "library" application rather than a "server" one):

1. All object calls/accesses are cross process. This can be a bad thing.

2. Object lifetime is controlled by the surrogate host, in this case, it provides JIT activation, object pooling (Assuming that your object supports it, which objects written in VB do not), etc. A caveat to this is that while VB objects housed in MTS/COM+ can't be reused, they are still "kept around" for the minimum object lifetime (I think the default is 3 minutes).

3. Process isolation, such that the DLL cannot thrash your application's memory space, and you can't thrash the objects memory space.

4. Transaction support is included, assuming that you implement the appropriate interfaces (ObjectControl), mark your objects as such, and code your objects to utilize the SetComplete/SetAbort methodology.

5. Ideally, objects should be stateless. This means that (again ideally) no object state should be retained between calls into the object. In practice, this tends to be impractical, so the reality is that you should try to limit the property/method access on your objects, and free (set them to nothing) as soon as is reasonable.

As for the reference counter bits, the VB runtime still controls those things AFAIK, and while MTS/COM+ tries to take a more active hold on them, you still have to deal with any idiosyncracies that the VB runtime inherits the way you would in standard VB.

TheJet
 

DaleDBeast

Wise, Aged Ars Veteran
172
...does the COM+ counter simply wrap around VB's broken counter?...

You might find this strange, but on the occasions where I have given thought to whether VB's reference counter is "broken" or not, I have always concluded that it actually works pretty well. This mechanism has existed at least since VB 4, and is deeply interwoven into the environment. The failings I and others have mentioned are things that tend to show up at the extreme uses of the subsystem, where you must after all, be a programmer.
 

earl

Well-known member
3,066
Let me echo DaleDBBeast. A medium sized VB app was crashing whenever we exited the app and sometimes on loading new data files. The fix, it seams, was manually deleting the in core representation of the data files, which tended to be about 15 MB over several hundred to several thousand objects. Why this fixed the crashing I don't know, but I think it was shitty garbage collection.

-earl-
 
Status
Not open for further replies.