Will this cause memory leak?
-
If I keep looping the folloing statement with the new pen with a timer , will it cause a memory leak? I know C++ when you create with new, you have to use delete. But for .NET C# / VB.NET, does this apply? Do I need to use the Dispose statement every time I use the new statement? Please Advise. mypen = New Pen(Color:=Color.DarkRed, Width:=1) g = Me.CreateGraphics g.DrawLine(mypen, 0, 235, 600, 235)
-
If I keep looping the folloing statement with the new pen with a timer , will it cause a memory leak? I know C++ when you create with new, you have to use delete. But for .NET C# / VB.NET, does this apply? Do I need to use the Dispose statement every time I use the new statement? Please Advise. mypen = New Pen(Color:=Color.DarkRed, Width:=1) g = Me.CreateGraphics g.DrawLine(mypen, 0, 235, 600, 235)
Most likely the
Dispose
orClose
method closes or releases unmanaged resources which don't underlie the control of the GC. If the component is well programmed it provides a finalizer which ensures that the unmanaged resources are freed when the call to Dispose was forgotten. As the finalization has an impact on performance it's recommended to call Dispose or Close. Both will suppress the finalization cause the unmanaged resources are already freed. Take a look at MSDN[^].
-
If I keep looping the folloing statement with the new pen with a timer , will it cause a memory leak? I know C++ when you create with new, you have to use delete. But for .NET C# / VB.NET, does this apply? Do I need to use the Dispose statement every time I use the new statement? Please Advise. mypen = New Pen(Color:=Color.DarkRed, Width:=1) g = Me.CreateGraphics g.DrawLine(mypen, 0, 235, 600, 235)
alternatively you could do:
using (Pen mypen = new Pen(Color.DarkRed, 1))
{
g = this.CreateGraphics();
g.DrawLine(mypen, 0, 235, 600, 235);
}
website // Project : AmmoITX //profile Another Post by NnamdiOnyeyiri
-
If I keep looping the folloing statement with the new pen with a timer , will it cause a memory leak? I know C++ when you create with new, you have to use delete. But for .NET C# / VB.NET, does this apply? Do I need to use the Dispose statement every time I use the new statement? Please Advise. mypen = New Pen(Color:=Color.DarkRed, Width:=1) g = Me.CreateGraphics g.DrawLine(mypen, 0, 235, 600, 235)
Memory leak - no. GDI handle leak - yes.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.