Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Good programming - memory allocation

Good programming - memory allocation

Scheduled Pinned Locked Moved Visual Basic
csharpdatabaseperformancequestiondiscussion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sled Dog
    wrote on last edited by
    #1

    Coding Dogs, I would like to know that I am programming correctly & responsibly. Would you say that the new version of this function is correct as it concerns allocating and deallocating new objects? Or do I even have to bother with this? a.k.a. Can I just rely on the .NET environment to handle memory management once a DIM goes out of scope? I am a C# programmer and I feel like the NEW VERSION is correct, but I am not sure. Also, I did not like variables declared on the fly. Thank you in advance for any opinions. William-SD :confused: 'NEW VERSION' Public Shared Function GetData(ByRef SQL As String) As System.Data.DataSet Dim cn As System.Data.SqlClient.SqlConnection Dim adp As System.Data.SqlClient.SqlDataAdapter Dim ds As System.Data.DataSet = New System.Data.DataSet Try cn = New System.Data.SqlClient.SqlConnection(ConnectionString) If Not (cn.State = ConnectionState.Open) Then Throw New System.Exception("Database connection failed") End If adp = New System.Data.SqlClient.SqlDataAdapter(SQL, cn) If (Len(SQL) > 0) Then cn.Open() adp.Fill(ds) GetData = ds Else ds.Dispose() ds = Nothing GetData = New System.Data.DataSet 'return nothing in an empty dataset End If Catch ds.Dispose() ds = Nothing GetData = New System.Data.DataSet 'return nothing in an empty dataset Finally cn.Close() adp.Dispose() cn.Dispose() adp = Nothing cn = Nothing End Try End Function 'OLD VERSION' Public Function GetData(ByRef SQL As String, ByRef ConnectionString As String) As System.Data.DataSet Dim ds As System.Data.DataSet = New System.Data.DataSet If (Len(SQL) > 0) Then Dim cn As System.Data.SqlClient.SqlConnection = Connect(ConnectionString) Try Dim cmd As SqlClient.SqlCommand = cn.CreateCommand() cmd.CommandText = SQL cmd.CommandType = CommandType.Text Dim adp As System.Data.SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(cmd) adp.Fill(ds) Finally cn.Close() End Try End If GetData = ds End Function

    T S 2 Replies Last reply
    0
    • S Sled Dog

      Coding Dogs, I would like to know that I am programming correctly & responsibly. Would you say that the new version of this function is correct as it concerns allocating and deallocating new objects? Or do I even have to bother with this? a.k.a. Can I just rely on the .NET environment to handle memory management once a DIM goes out of scope? I am a C# programmer and I feel like the NEW VERSION is correct, but I am not sure. Also, I did not like variables declared on the fly. Thank you in advance for any opinions. William-SD :confused: 'NEW VERSION' Public Shared Function GetData(ByRef SQL As String) As System.Data.DataSet Dim cn As System.Data.SqlClient.SqlConnection Dim adp As System.Data.SqlClient.SqlDataAdapter Dim ds As System.Data.DataSet = New System.Data.DataSet Try cn = New System.Data.SqlClient.SqlConnection(ConnectionString) If Not (cn.State = ConnectionState.Open) Then Throw New System.Exception("Database connection failed") End If adp = New System.Data.SqlClient.SqlDataAdapter(SQL, cn) If (Len(SQL) > 0) Then cn.Open() adp.Fill(ds) GetData = ds Else ds.Dispose() ds = Nothing GetData = New System.Data.DataSet 'return nothing in an empty dataset End If Catch ds.Dispose() ds = Nothing GetData = New System.Data.DataSet 'return nothing in an empty dataset Finally cn.Close() adp.Dispose() cn.Dispose() adp = Nothing cn = Nothing End Try End Function 'OLD VERSION' Public Function GetData(ByRef SQL As String, ByRef ConnectionString As String) As System.Data.DataSet Dim ds As System.Data.DataSet = New System.Data.DataSet If (Len(SQL) > 0) Then Dim cn As System.Data.SqlClient.SqlConnection = Connect(ConnectionString) Try Dim cmd As SqlClient.SqlCommand = cn.CreateCommand() cmd.CommandText = SQL cmd.CommandType = CommandType.Text Dim adp As System.Data.SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(cmd) adp.Fill(ds) Finally cn.Close() End Try End If GetData = ds End Function

      T Offline
      T Offline
      T Smooth
      wrote on last edited by
      #2

      I'm not sure which way is necessarily better but in both of your finally clauses, if cn fails to be allocated or open for some reason and is nothing then you will get an exception when you do cn.Close(). I've made this same mistake a few times and usually i just fix it like this: If Not cn Is Nothing Then cn.Close() - Tom

      S 1 Reply Last reply
      0
      • T T Smooth

        I'm not sure which way is necessarily better but in both of your finally clauses, if cn fails to be allocated or open for some reason and is nothing then you will get an exception when you do cn.Close(). I've made this same mistake a few times and usually i just fix it like this: If Not cn Is Nothing Then cn.Close() - Tom

        S Offline
        S Offline
        StylezHouse
        wrote on last edited by
        #3

        The dispose methods does that for you. Simple call cn.Dispose() in your finally.

        A 1 Reply Last reply
        0
        • S Sled Dog

          Coding Dogs, I would like to know that I am programming correctly & responsibly. Would you say that the new version of this function is correct as it concerns allocating and deallocating new objects? Or do I even have to bother with this? a.k.a. Can I just rely on the .NET environment to handle memory management once a DIM goes out of scope? I am a C# programmer and I feel like the NEW VERSION is correct, but I am not sure. Also, I did not like variables declared on the fly. Thank you in advance for any opinions. William-SD :confused: 'NEW VERSION' Public Shared Function GetData(ByRef SQL As String) As System.Data.DataSet Dim cn As System.Data.SqlClient.SqlConnection Dim adp As System.Data.SqlClient.SqlDataAdapter Dim ds As System.Data.DataSet = New System.Data.DataSet Try cn = New System.Data.SqlClient.SqlConnection(ConnectionString) If Not (cn.State = ConnectionState.Open) Then Throw New System.Exception("Database connection failed") End If adp = New System.Data.SqlClient.SqlDataAdapter(SQL, cn) If (Len(SQL) > 0) Then cn.Open() adp.Fill(ds) GetData = ds Else ds.Dispose() ds = Nothing GetData = New System.Data.DataSet 'return nothing in an empty dataset End If Catch ds.Dispose() ds = Nothing GetData = New System.Data.DataSet 'return nothing in an empty dataset Finally cn.Close() adp.Dispose() cn.Dispose() adp = Nothing cn = Nothing End Try End Function 'OLD VERSION' Public Function GetData(ByRef SQL As String, ByRef ConnectionString As String) As System.Data.DataSet Dim ds As System.Data.DataSet = New System.Data.DataSet If (Len(SQL) > 0) Then Dim cn As System.Data.SqlClient.SqlConnection = Connect(ConnectionString) Try Dim cmd As SqlClient.SqlCommand = cn.CreateCommand() cmd.CommandText = SQL cmd.CommandType = CommandType.Text Dim adp As System.Data.SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(cmd) adp.Fill(ds) Finally cn.Close() End Try End If GetData = ds End Function

          S Offline
          S Offline
          StylezHouse
          wrote on last edited by
          #4

          I would write it like this: If you need an explaination on any reason why I did certain things, just let me know. Public Shared Function GetData(ByRef SQL As String) As System.Data.DataSet Dim cn As New System.Data.SqlClient.SqlConnection(ConnectionString) Dim adp As New System.Data.SqlClient.SqlDataAdapter(SQL, cn) Dim ds As New System.Data.DataSet Try If SQL.Length > 0 Then cn.Open() adp.Fill(ds) End If Finally adp.Dispose() cn.Dispose() End Try Return ds End Function

          T 1 Reply Last reply
          0
          • S StylezHouse

            The dispose methods does that for you. Simple call cn.Dispose() in your finally.

            A Offline
            A Offline
            Anonymous
            wrote on last edited by
            #5

            i think cn.dispose() is better

            1 Reply Last reply
            0
            • S StylezHouse

              I would write it like this: If you need an explaination on any reason why I did certain things, just let me know. Public Shared Function GetData(ByRef SQL As String) As System.Data.DataSet Dim cn As New System.Data.SqlClient.SqlConnection(ConnectionString) Dim adp As New System.Data.SqlClient.SqlDataAdapter(SQL, cn) Dim ds As New System.Data.DataSet Try If SQL.Length > 0 Then cn.Open() adp.Fill(ds) End If Finally adp.Dispose() cn.Dispose() End Try Return ds End Function

              T Offline
              T Offline
              T Smooth
              wrote on last edited by
              #6

              Does Dispose() work if the variable is never initialized? Example: Dim MyObject As Object MyObject.Dispose() I suppose I could test it quick but does that throw an exception because of a null object reference? Edit: Ok tested with following code Dim oObject As New Object oObject = Nothing oObject.Dispose() Which threw a NullReferenceException. This is why I was wondering if it was safe to just put cn.Dispose() in the finally clause instead of enclosing it in If Not cn Is Nothing Then block. That way you are covered if for some reason it never got initialized. Unless I'm missing something.

              D 1 Reply Last reply
              0
              • T T Smooth

                Does Dispose() work if the variable is never initialized? Example: Dim MyObject As Object MyObject.Dispose() I suppose I could test it quick but does that throw an exception because of a null object reference? Edit: Ok tested with following code Dim oObject As New Object oObject = Nothing oObject.Dispose() Which threw a NullReferenceException. This is why I was wondering if it was safe to just put cn.Dispose() in the finally clause instead of enclosing it in If Not cn Is Nothing Then block. That way you are covered if for some reason it never got initialized. Unless I'm missing something.

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                Don't worry about the whatever = Nothing. Just call the .Dispose() method on it. As soon as the object goes out of scope, the reference is automatically dropped and the GC collects it, when it gets around to it. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                T 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  Don't worry about the whatever = Nothing. Just call the .Dispose() method on it. As soon as the object goes out of scope, the reference is automatically dropped and the GC collects it, when it gets around to it. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  T Offline
                  T Offline
                  T Smooth
                  wrote on last edited by
                  #8

                  I realize you don't have to set an object reference to nothing. That wasn't my point. The point was you can't call .Dispose() on an NullReference. So if for some reason the object never gets assigned a reference (say there is a memory allocation problem for some reason) then in the finally clause the program would crash if there is a object.dispose() statement.

                  S 1 Reply Last reply
                  0
                  • T T Smooth

                    I realize you don't have to set an object reference to nothing. That wasn't my point. The point was you can't call .Dispose() on an NullReference. So if for some reason the object never gets assigned a reference (say there is a memory allocation problem for some reason) then in the finally clause the program would crash if there is a object.dispose() statement.

                    S Offline
                    S Offline
                    StylezHouse
                    wrote on last edited by
                    #9

                    There is no NullReference Exception in my example, I declare the variables as new at the top of the sub. If there's a memory allocation problem at some point that prevents the variable from being initialized, then the user has way more problems than your app crashing :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups