Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Class Object Termination Problem....

Posted on 1998-07-29
8
Medium Priority
?
150 Views
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....

Pete
0
Comment
Question by:peterwest
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

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

   Set yourObj = Nothing
0
 
LVL 2

Author Comment

by:peterwest
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.

Pete
0
 

Accepted Solution

by:
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.
0
Industry Leaders: 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!

 
LVL 2

Author Comment

by:peterwest
ID: 1467066
diccon,

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...

Pete


0
 

Expert Comment

by:diccon
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?
0
 
LVL 2

Author Comment

by:peterwest
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.

Pete

0
 

Expert Comment

by:diccon
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.
0
 
LVL 2

Author Comment

by:peterwest
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.

Pete

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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

886 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