Re: closing DB connections

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Yeah, I remember now. I see that I'm now in the process of fast forgetting
everything that I knew about Visual Basic; probably to make some place for
the new technologies. Good riddance!

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)


"Robert Morley" <rmorley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:%23rL9oAWMHHA.5064@xxxxxxxxxxxxxxxxxxxxxxx
Yes, the GC in .Net is immune to this, since it's not based on reference
counting. The garbage collection in VB6/VBA is much more primitive and is
subject to the circular reference problem, since it IS based on reference
counting. See: http://support.microsoft.com/kb/189156/en-us and a
similar problem in this article:
http://support.microsoft.com/kb/190519/en-us.



Rob

"Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
wrote in message news:Ofy9%236UMHHA.2028@xxxxxxxxxxxxxxxxxxxxxxx
About your exemple with Parent and child collections, GC are immune to
this kind of mutual relationship; this is why they are used. You can
find a good explanation about this process in the following two-part
article:

http://msdn.microsoft.com/msdnmag/issues/1100/GCI/default.aspx
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/

Of course, this is about the GC in the .NET framework (notice that some
of the mecanisms and functions explained in this article are for NET 1.0
and are no longer valid for 1.1 and 2.0) and cannot tell us so much about
the one used in VBA; however, I suspect that many conclusions that we can
draw from it will apply to VBA, too.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: sylvain aei ca (fill the blanks, no spam please)


"Robert Morley" <rmorley@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:Orm2AsPMHHA.1044@xxxxxxxxxxxxxxxxxxxxxxx
Hehehe...I was brought up on Pascal. I *never* use an Exit Sub within a
With block, or similar construct, and there's only ever one Exit Sub (if
any) in each procedure. I don't even use Exit For and the like; I've
never found a need to do so.

But to your point, I suspect that with something this relatively
straight-forward, the garbage collector would have no problems cleaning
it up. Where it becomes a problem is when you have parent/child objects
where the parent has a child collection, and the children have a parent
object. Since the objects have a mutual relationship, de-referencing the
objects themselves doesn't properly decrement the reference
counter...you have to manually set the Parent property to Nothing for
all child objects (or similar coding, depending on how you've set things
up, exactly). This is one instance where I know the GC will fail, there
are probably others.

This also leads into your point about why you would clean things up
yourself when the GC can do it for you (with the exception of the
above). As you've pointed out, one reason not to wait for the GC is to
conserve resources be de-allocating earlier on in your code, or to
choose the order and/or timing of de-referencing if that's important to
you for whatever reason, or due to simple distrust of automated
processes...some people like to ensure that things ARE cleaned up
properly, rather than trusting that the GC will figure it all out
properly. Also, if you have a bug in your tear-down code (i.e., object
destruction/termination/whatever) and the GC is handling it, it may be
more difficult to debug, or may not respond as you'd want it to in the
event of an error (such as logging the error to a file/table).

You're right that many people don't think of cleaning up strings, but I
can see at least some argument for that, in that strings are "simple"
objects--there's no tear-down for them beyond de-allocating the memory
that they've used. Often for more complex objects, there is. That
said, if you've got an object that allocates only a few bytes of memory
and has no tear-down vs. a string that's tens or hundreds of kilobytes
long, and you're concerned about resources, then obviously it's to your
benefit to worry about the string long before the object.


Rob

<decland@xxxxxxxxx> wrote in message
news:1167986425.303978.157750@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Robert Morley wrote:

But rs1 is already defined & reference-counted...why would using With
count
it again? I'm not saying it doesn't, but I've never heard/read about
With
doing that. Even assuming you're right and it does, wouldn't the End
With
destroy the reference?

You are correct about End With, of course; I fudged the code! Try this
revision:

Sub Test()
Dim rs1 As Object
Set rs1 = CreateObject("ADODB.Recordset")
With rs1
.Fields.Append "Test", 200, 12
.Open
.AddNew "Test", "Test"

Dim rs2
Set rs2 = rs1
Exit Sub

End With
rs1.Close
End Sub

My point was supposed to be: at the Exit Sub line the reference count
will be three and *within* the With block you cannot release the third
reference.

Jamie.

--









.



Relevant Pages

  • Re: closing DB connections
    ... With block, or similar construct, and there's only ever one Exit Sub (if ... themselves doesn't properly decrement the reference counter...you have to ... You're right that many people don't think of cleaning up strings, ... Dim rs1 As Object ...
    (microsoft.public.access.adp.sqlserver)
  • Re: closing DB connections
    ... Yes, the GC in .Net is immune to this, since it's not based on reference ... The garbage collection in VB6/VBA is much more primitive and is ... You're right that many people don't think of cleaning up strings, ... Dim rs1 As Object ...
    (microsoft.public.access.adp.sqlserver)
  • Re: Complex Specified Information - Pitman Formula
    ... between the reference string and the test string. ... I *know* what sequences actually exist or possibly could ... There are only test strings that you compare to ...
    (talk.origins)
  • Re: Complex Specified Information - Pitman Formula
    ... between the reference string and the test string. ... I *know* what sequences actually exist or possibly could ... There are only test strings that you compare to ...
    (talk.origins)
  • Re: owner of class
    ... >> terminate a given object but because I've created circular reference, ... > Public Sub AttachParent ... > Then each class you need to know it's parent object can simply implement ... > Private Sub IParentTrack_AttachParent ...
    (microsoft.public.vb.general.discussion)