Results 1 to 7 of 7

Thread: How are objects destroyed in VB6 ?

  1. #1

    Thread Starter
    Registered User
    Join Date
    Sep 2002
    Posts
    14

    How are objects destroyed in VB6 ?

    Hello,

    I have a main app and two classes. The first class gets an ADO recordset from an Access2000 DB. The second class generates
    a csv file from an ADO recordset.

    So my main app asks class1 for a recordset and feeds it into
    class2 which is working fine.

    I am just wondering if the recordset object is being properly destroyed because it first gets instantiated in class 1 and then
    passed by reference all over the project (sometimes using "local" references to the same recordset).

    I saw in VB6 help that if you have multiple references to 1 object, the object gets released if you set the last ref. to nothing.

    OK; but what about passing references ??
    Does anyone have a clear explanation for this ?

    THANKS,
    Jukke

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    You need to look up byval & byref calls for a better explanation, but the general idea is:

    • ByRef - you pass the actual recordset variable across to the procedure.
    • ByVal - vb makes a virtual copy of the recordset & passes this into the procedure (hance any changes you make don't affect the original recordset).


    So if you set a value in one procedure, then pass this value using Byval to a second procedure and make a call to destroy it, you are destroying the actual value itself:

    VB Code:
    1. Private Sub form_load()
    2.  
    3.    set rs = new recordset reference created & set
    4.    call myProcedure
    5.  
    6. End Sub
    7.  
    8.  
    9. Private Sub myprocedure(byval myrs as adodb.recordset)
    10.  
    11.     msgbox rs!FieldValue
    12.     rs.close
    13.     set rs = nothing
    14.  
    15. End sub
    Last edited by alex_read; Oct 14th, 2002 at 02:53 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Alex, you have your Byval and ByRef explanation backwards.

  4. #4
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Damn, I always do that. Sorry for any confusion - I've updated the above.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  5. #5
    Lively Member
    Join Date
    Oct 2002
    Location
    Los Angeles, CA
    Posts
    73
    I think you are a little bit confused here.

    When you pass object byVal you do not pass a copy of this object, but only a pointer copy

    This link will give you explanation.


    http://www.vb2themax.com/Item.asp?Pa...Cat=500&ID=278

  6. #6
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    All object variables in VB are pointers to objects. What they really are: function pointers that point to the first element of an array of functions (vtable).

    Set object=Nothing may destroy an object - ie., the SCM unmaps the memory used by the object. BUT. It only does this if the number of references to the object (by the thread or process)
    goes to zero.

    Code:
    Dim w as object, x as object
    Set w= New ListBox
    Set x=w
    In this case, Set x= Nothing destroys the object because only ONE version of the object was created. x is a copy of w.

  7. #7
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    learn something new every day!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width