Class Object Termination Problem....

Posted on 1998-07-29
Last Modified: 2010-05-03
Hi there,

I've written a program that allows users to telnet to it; as each connection is made a new instance of a telnet session object is created, which is part of a collection class.

This all appeared to be working fine; objects were created for each session and then subsequently released when the client disconnected.  However, i've found a somewhat annoying bug in the program that I shall now explain.

1. Three sessions log on  - three objects are created
2. Session 1 disconnects - object is removed from collection
3. Session 1 reconnects - object is created
4. Session 1 disconnects - object is removed from collection
5. Session2 disconnects - object is removed from collection BUT - Class_Terminate event isn't fired - why???

As you can see from the above, when you perform certain actions such as these the class_terminate event isn't fired.  If, however, you then invoke any method of the class object, even if it's for a different object, then the class_terminate event is fired - it's almost as if it knows it's got to fire the event but it waits for you to do something before it continues....

Anyone got any ideas??  The class collection and objects are part of an ActiveX EXE that is used by a child programme too....

Question by:peterwest
  • 4
  • 3
LVL 14

Expert Comment

ID: 1467063
I think, you have to set your object to Nothing. Unless the object remains in memory

   Set yourObj = Nothing

Author Comment

ID: 1467064
I know about setting a reference to an object to nothing; this would be performed by the objects client program and not by the object itself - it doesn't explain, however, why the class_terminate event isn't been called in all circumstances.


Accepted Solution

diccon earned 200 total points
ID: 1467065
You don't make it clear just when Terminate events _do_ fire. It sounds to me that, if you're saying that the object's client will set to Nothing (not true - the Remove method of the collection should do that), you've probably messed up the references.
If your client is retaining a reference to a session whilst manipulating the collection, this would explain your problem.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1467066

I've checked through the code and all object references are being terminated in the correct place.  I've actually found the solution to the problem after much headscratching and am a little perturbed as to why it should cause a problem - anyway i've worked a way round the problem.

I was using the BEFORE and AFTER sections of the collection.add method to insert the collection items into their appropriate locations - the idea being that I wanted to keep the collection items in order so that when I wanted to find a object key I could simply search through the collection until I found a gap - because the items were in order I could terminate the search, safe in the knowledge that the key i was going to use was unique.

Anyway, i've now re-written the routine that allocates the key for the object - it's a little more inefficient but it means the collection items don't need to be in order - it all works fine now.

Looks like the before and after parameters of the add method were causing some weird problems...



Expert Comment

ID: 1467067
Before and after have no published problems that I know of. Some clue as to the code you're using would help. Are you using numeric keys in your collection?

Author Comment

ID: 1467068
Hi again,

The keys I am using are based on numeric values that are then converted to string representations using the CStr function.  I've used this method because each object relates to an element in a control array; the key ties directly to the index value of the control.

If you want I can post the code that creates an instance of the object and also the code that removes the object.



Expert Comment

ID: 1467069
Ah, well be careful. You may have converted your keys to strings but they are passed through as variants and collections won't pay any heed to the _type_ of variant - just whether it's numeric or not (see IsNumeric).

If it is numeric, the key is taken to be an index number (position) in the collection. If it isn't, it's taken to be an access key.

Adding to a collection using Before and After with index numbers has a different effect on the resultant order from that using keys.

For example, If you have 3 items to add to a collection with keys "A", "C" and "B" respectively and you add A, then C, then B before C, the resultant order will be A-B-C as expected. If the keys were "1", "3" and "2" and you add "1" (at position 1), then "3" (at position 2) then "2" before "3" (there is no position 3 so insertion takes place at the end) and the resultant order will be 1-3-2.

Author Comment

ID: 1467070
Hi Diccon,

Well - although i've circumnavigated the problem it sounds like what you're suggesting has been the cause of the problem.

I'll bear this in mind for the future - thanks for your time.



Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 67
VBA Shell can't Find Word document 11 130
Help me. 3 69
VB6 calling COM DLL written in Visual Studio 2003 6 10
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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

749 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