Results 1 to 9 of 9

Thread: Correct me if i'm wrong...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    276

    Correct me if i'm wrong...

    Hi,
    Consider the following code:
    VB Code:
    1. Set oRs = CallMethodToGetRecordset()
    2.  Set oRs1 = oRs
    3.  Set oRs = Nothing
    All along I'v been under the impression that setting oRs to Nothing within the scope would set oRs1 also to Nothing. But today I see that oRs1 has its records in place! I wondor how...

    Is it something like this?
    The statement "Set oRs1 = oRs" will set oRs1 to the Base Address of oRs. If I set the base address of oRs to Nothing, oRs1 is still pointing to the location with the values.

    However, when I move oRs, oRs1 is also getting moved. When i close oRs, oRs1 is also getting closed!
    Can some one pls explain...

    - Jemima.

  2. #2
    Hyperactive Member abhid's Avatar
    Join Date
    Nov 2001
    Location
    3rd rock from the sun
    Posts
    467
    this is like two pointers pointing to same location.
    oRs1 is pointing to oRs that is why it gets moved when you move Ors. but when you set ors1 to nothing only reference of ors gets broken and memory is freed. ors is still there!
    it is like
    ors1->ors->some records

  3. #3
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    As far as I know the technique is used in case of Disconected recordsets wherein u need to ease the server load

    How it works actually in the background - I think the same what u have written - cant explained it otherwise

  4. #4
    somnath
    Guest
    While writing a Clone method in my class module which
    contain a objects called say oX of type X, I had got the same
    problem.

    I solved it by creating a new local copy of object X in Clone method and assigned the properties of oX to X using '='

    I think the problem in your case can be solved by creating
    an intermediate for marshalling purpose, so that
    any changes made by new object should not reflect in the old
    object

    Hope this won't complicates your matter

    - Somnath

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    276
    No Abhid... i don't still get the point... Yes, setting an object to nothing releases the memory taken by the object, a recordset here, then where is oRs1 pointing? Directly to the base address in memory? In that case, how about Moving the recordset?

    Thanks, somnath but i don't have a practical situation right now, jus curious to know.

    Thanks guys.
    Jemima.

  6. #6
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Let's put it this way:

    The OpenRecordset method creates an object of type Recordset and passes its reference back. You have a recordset object in which you store this reference. That goes something like:

    Set rs1 = db.OpenRecordset("...")

    (I guess here the record values are loaded into the memory. And I missed another point: You are using your own method to create and pass a recordset object. It's got to be only a reference, not an actual set of values!)

    Here rs1 only contains a reference to the RecordSet object created by OpenRecordset.

    Now you create another recordset object rs1, and assign the value of rs1 to rs2. So you assign the reference to the recordset to rs2.

    Now when you take any actions that affect the recordset, such as moving or closing, since they will affect the recordset, they will be reflected in both rs1 and rs2.

    However, setting rs1 to Nothing will only free up this reference object. The recordset is still in the memory, and its address has already been stored to the rs2 variable. So, rs2 still maintains its link to the recordset.

    I think that's why you need to Close the recordset before setting it to Nothing. Close ensures that the recordset is actually removed from the memory, and setting a recordset variable to Nothing will make sure that the reference to that memory location has been removed. Sort of like the garbage collection in Java.

    mmmmm.... I think that's pretty much how it works. Or should work.



    .
    Last edited by honeybee; Dec 14th, 2001 at 06:28 AM.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  7. #7
    Hyperactive Member abhid's Avatar
    Join Date
    Nov 2001
    Location
    3rd rock from the sun
    Posts
    467
    when you use Set to assign an object reference to a variable, no copy of the object is created for that variable. Instead, a reference to the object is created

    Found this in MSDN. I think this means even after
    Set oRs = CallMethodToGetRecordset()
    Set oRs1 = oRs
    ors1 still refers to recordset returned by method and not to ors.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    India
    Posts
    276
    Hi Hb.
    Yeah getting it now... I think thats what abhid was telling all the while! Tuk some time to get in here, though! Thanks.
    So its really good habits to close a recordset and then release it! Will keep that in mind.
    You've enlightened me! Thanks

    Jemima.

  9. #9
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Gee, thanks.

    I was afraid I would lose track of the discussion. It was such a long post on such a complex topic!! Thank God it came out alright.



    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

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