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 8197517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:45:29+00:00 2026-06-07T05:45:29+00:00

I have seen some code like this (not on consecutive lines, but in the

  • 0

I have seen some code like this (not on consecutive lines, but in the same function scope):

Dim con1 As SqlConnection
con1 = New SqlConnection
'More code here
con1 = New SqlConnection
con1 = Nothing

I believe this is just a bug, but I wanted to check that there is not a form of shadowing going on here that I am unaware of. What happens to the first con1 variable? I assume it is inaccessible as there is no reference to the object.

  • 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-06-07T05:45:30+00:00Added an answer on June 7, 2026 at 5:45 am

    What’s Happening Here

    con1 points to two different objects during the lifetime of that function.

    The first object, created by the first

    con1 = New SqlConnection
    

    is no longer referenced after the second

    con1 = New SqlConnection
    

    is executed.

    Is this a memory leak?

    No. The object that is no longer referenced will eventually be disposed of, then the GC decides to do so. However it is a resource leak. Every time you fail to close a SQL Connection (assuming it was opened and not just allocated), you leave a resource unavailable for reuse. The GC will trigger when memory is low, so you will certainly regain the unreferenced object’s memory at the latest when the system is low on memory (you will also regain the DB connection at that point). However, low resources will not trigger GC. You could run completely out of DB connections before the GC ever decides to kick in and release the SqlConnection objects (including the DB connections they were hoarding).

    Fixing the Code

    Since SqlConnection must be closed to release the connection, the first object will hang around until the GC decides to dispose of it. That is a bad thing, as SQL connections are a resource that should only be held as long as necessary.

    Calling Close() the first connection before assigning the new SqlConnection object would improve the situation (also, call Close() on the second instance before leaving variable scope).

    However, if you get an Exception somewhere in the code, without proper exception handling, you would still be left with undisposed objects until the GC kicks in. Always, always put exception handling around anything that manages a resource such as this.

    The very best way to put exception handling in place for this scenario is with the Using keyword. I have never written a line of VB.Net until now, but here’s my attempt at a correct version of your code:

    Dim con1 As SqlConnection
    
    Using con1
        con1 = New SqlConnection
    End Using
    
    'More code here
    
    Using con1
        con1 = New SqlConnection
    End Using
    
    ' NOTE: I believe the following is unnecessary, but was necessary in VB6 and prior
    ' con1 = Nothing 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen in some source code (by other developers) something like this: #import
I have seen some of the other answers on this topic but dont really
I have some code in my application that looks something like this: char *hash
I want to know the meaning of the code \. I have seen some
I have seen some related questions but none focusing on the specific problem I
i have seen some hand rolled solutions but does jquery out of the box
This question is similar to my previous question but not the same ... please
The answer is probably obvious but I have not seen anything I could use
I have a ko.computed() function that proxies a collection like this: function ListView(query) {
I've seen some sample code that does this: proc_entry->read_proc = module_read; proc_entry->write_proc = module_write;

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.