Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

Class Object Termination Problem....

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
peterwest
Asked:
peterwest
  • 4
  • 3
1 Solution
 
watyCommented:
I think, you have to set your object to Nothing. Unless the object remains in memory

   Set yourObj = Nothing
0
 
peterwestAuthor Commented:
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
 
dicconCommented:
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
Independent Software Vendors: 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!

 
peterwestAuthor Commented:
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
 
dicconCommented:
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
 
peterwestAuthor Commented:
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
 
dicconCommented:
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
 
peterwestAuthor Commented:
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now