Object deleted or memory leak?

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

If I create an object (instantiate) then do it again using the same Pointer,
will I leave the old object hanging around wasting memory?

Would this create a memory leak or would it be taken care of when I leave
the function?

**********************************************************
Sub ActiveJobEdit_Click(s as Object, e as ImageClickEventArgs)
Dim newPosition as Position = new Position(oLabel.Text)
....
newPosition = new Position(otherLabel.Text)
session("newPosition) = newPosition
End Sub
**********************************************************

Thanks,

Tom
 
the great thing about VB is that (almost?) all of your memory management is
takin care of for you. the GC comes and takes care of any unused objects
every so often, once u exit the sub, all local variables are GCed
 
tshad said:
If I create an object (instantiate) then do it again using the same Pointer,
will I leave the old object hanging around wasting memory?

Would this create a memory leak or would it be taken care of when I leave
the function?

**********************************************************
Sub ActiveJobEdit_Click(s as Object, e as ImageClickEventArgs)
Dim newPosition as Position = new Position(oLabel.Text)
...
newPosition = new Position(otherLabel.Text)
session("newPosition) = newPosition
End Sub
**********************************************************

Thanks,

Tom

What you are doing would not create a memory leak - but, it also
wouldn't be taken care of when you leave the function... At least,
probably not :)

See, object life time in .NET is non-deterministic. It is controled by
the garbage collector, and that generally only runs when your
application is under memory pressure. Here is an article that
gives a rundown of Memory management in .NET

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

The code is C# and the articles are a bit old, but it covers most of
the basics. You can also read the documentation, for more details.
 
Tshad,

Because there are two answers sent in the same time, I would keep it by the
answer from Iwdu15, and if you have a program that is working in the area of
science or something like that, than read the message from Tom.

Just my thought,

Cor
 
Back
Top