Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 426129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:26:55+00:00 2026-05-12T19:26:55+00:00

I apologize in advance; this is a long question. I’ve tried to simplify as

  • 0

I apologize in advance; this is a long question. I’ve tried to simplify as much as I can but it’s still a bit more long-winded than I’d care to see.

In some legacy code, we’ve got a VB6 collection. This collection adds objects via the .Add method and removes them via the .Remove method. However, via tracing I can see that sometimes when the .Remove is called it appears that the class terminate for the object isn’t called. But it’s not consistent; it happens only rarely and I can’t isolate the circumstances under which it fails to fire the class terminate.

Consider the following demonstration code:

Option Explicit
Private Const maxServants As Integer = 15
Private Const className As String = "Master"
Private Sub Class_Initialize()
    Debug.Print className & " class constructor "
    Set g_coll1 = New Collection
    Dim i As Integer
    For i = 1 To maxServants
        Dim m_servant As Servant
        Set m_servant = New Servant
        m_servant.InstanceNo = i
        g_coll1.Add Item:=m_servant, Key:=CStr(i)
        Debug.Print "Adding servant " & m_servant.InstanceNo
    Next
End Sub
Private Sub Class_Terminate()
    Dim i As Integer

    For i = maxServants To 1 Step -1
        g_coll1.Remove (CStr(i))
    Next

    Debug.Print className & " class terminator "
    Set g_coll1 = Nothing
    Exit Sub

End Sub

and

Option Explicit
Private Const className As String = "Servant"
Private m_instanceNo As Integer
Private Sub Class_Initialize()
    m_instanceNo = 0
    Debug.Print className & " class constructor "
End Sub
Public Property Get InstanceNo() As Integer
    InstanceNo = m_instanceNo
End Property
Public Property Let InstanceNo(newInstanceNo As Integer)
    m_instanceNo = newInstanceNo
End Property
Private Sub Class_Terminate()
    Debug.Print className & " class terminator for " & CStr(Me.InstanceNo)
End Sub

and this is the test harness code:

Option Explicit
Global g_coll1 As Collection
Public Sub Main()
    Dim a As Master
    Set a = New Master
End Sub

Now, for every run, the class_terminate of Servant is always invoked. And I can’t see anything in the production code which should keep the object in the collection referenced.

1.) Is there any way to force the class terminate on the Remove? That is, can I call Obj.Class_Terminate and be assured it will work every time?

2.) On my production code (and my little test app) the classes are marked “Instancing – 5 MultiUse”. I realize this may be some sort of threading issue; is there an effective way to prove (or disprove) that multi-threading is the cause of this issue–some sort of tracing I might add or some other sort of test I might perform?


EDIT: Per MarkJ’s insightful comment below, I should add that the test posted above and the production code are both ActiveX exe’s–part of the reason I ask about mulit-threading.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T19:26:55+00:00Added an answer on May 12, 2026 at 7:26 pm

    We had a similar issue, but where we could trace the non-termination of the objects to be down to an instance being held elsewhere in our application.

    In the end, we had to write our Termination method like this:

    Private Sub Class_Terminate()
        Terminate
    End Sub
    
    Public Sub Terminate()
        'Do real termination in here'
    End Sub
    

    So whenever you really wanted the class to be terminated (i.e. when you call g_coll1.Remove), you can also call Terminate on the held object.

    I think that you could also make Class_Terminate public, but that’s a bit ugly in my opinion.

    Re your point (2), I think it’s very unlikely to be a threading issue, but I can’t think of a good proof/test off the top of my head. I suppose one very quite thing you can consider is: do you manually use threading in your application? VB6 doesn’t do much threading automatically… (see edit below)

    [Edit] MarkJ tells us that apparently building as an ActiveX application means that VB6 does automatically do threading. Someone else will have to explore the implications of this, since I wasn’t familiar with it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize in advance for the long-winded question but I wanted to make sure
I apologize in advance if this question seems remedial. Which would be considered more
Apologies in advance for the long-winded question. I'm really a database programmer, but have
Let me apologize in advance for the simplicity of this question (I heard Jeff's
I apologize for asking such a generalized question, but it's something that can prove
I apologise in advance if my question seems a bit misleading, but I'm trying
I'm sure this has already been asked and answered so I apologize in advance
I apologise in advance if this question isn't very specific. Would it be possible
I apologize in advance for the long post... I used to be able to
I am going to apologize in advance for being extremely vague, but my knowledge

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.