Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Class Object Termination Problem....

Posted on 1998-07-29
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

715 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