Question Setting Parm objects to nothing

VBasic

Member
Joined
Feb 21, 2008
Messages
5
Programming Experience
10+
'VB.Net 2005 code example with question to follow:
VB.NET:
'Updates Sequal Server 2005 database
'
'Populate Parms
'
oParm = oData.Parameter("ExternalFile")     'Returns new Parm object	
With oParm
	.Size = bBytes.Length - 1
	.DbType = DbType.Binary
	.SqlDbType = SqlDbType.Image
	.Value = CompressByteArray(bBytes)	
End With
ocolParms.Add(oParm, oParm.ParameterName)
oParm = Nothing				
'
oParm = oData.Parameter("ContentLength")    'Returns new Parm object
With oParm
	.SqlDbType = SqlDbType.Int
	.DbType = DbType.Int64
	.SqlDbType = SqlDbType.Int
	.Value = bBytes.Length - 1
End With
ocolParms.Add(oParm, oParm.ParameterName)
oParm = Nothing				
'
oParm = oData.Parameter("ContentType")    'Returns new Parm object
With oParm
	.Size = 50
	.DbType = DbType.String
	.SqlDbType = SqlDbType.VarChar
	.Value = "image/pjpeg"					
End With
ocolParms.Add(oParm, oParm.ParameterName)
oParm = Nothing				
'
'Run SQL
'
oData.Execute(strSQL, ocolParms, , , 150, True)
ClearCollection(ocolParms)


The above code is an excerpt from a routine that I use to update a Sequal Server database record.

The question I'm asking is, do I need to set the oParm object = nothing each time or not? I'm confused about what this does/does not do.

I read where it is not necessary to set objects to nothing and I also read where sometimes you do. I also have warning messages in the wizard generated code (converted from VB6 to VB.Net) that warns me about setting objects to "nothing".

The warning alerts me to the fact that the object may still "live" for awhile after being set to nothing. If true (I've no doubt that it's true), then I would think the above routine has a problem. Having said that, I must reveal that this routine works.
 
The question I'm asking is, do I need to set the oParm object = nothing each time or not? I'm confused about what this does/does not do.
No. It is a completely useless operation if youre then going to immediately set it to a new instance of an object..

The warning alerts me to the fact that the object may still "live" for awhile after being set to nothing. If true (I've no doubt that it's true), then I would think the above routine has a problem. Having said that, I must reveal that this routine works.

oParm is a reference to a block of bytes in memory. As soon as you unlink it from those bytes by setting oParm = Nothing, you cannot use oParm to refer to those bytes any more. When there are no other surviving references to those bytes anywhere in the program, they will be marked for deleteion by the garbage collector. There may be a further delay before the GC actually runs as it is an expensive op and only usually runs when the memory is needed.

As such, if, say oParm had a really important, sensitive bit of data in it, then technically someone could e.g. hibernate your laptop, retrieve the hibernate file and search it for the data. Because the data was still in memory and not destroyed, then it's available, it's just not referenced. In practice we dont really care about things like this, but i'm trying to illustrate that it's NOT the case that as soon as you set something to Nothing that it is instatly deleted from memory and the memory it consumed freed for use
 
it hurts your fingers to type it, your productivity to take the time to perform this useless op, and my eyes to read it.. but it doesnt hurt the program logic, to put it in, no ;)
 
Back
Top