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. C#
  4. Will this cause memory leak?

Will this cause memory leak?

Scheduled Pinned Locked Moved C#
csharpc++performancequestion
4 Posts 4 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.
  • M Offline
    M Offline
    mitreviper
    wrote on last edited by
    #1

    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)

    S N N 3 Replies Last reply
    0
    • M mitreviper

      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)

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Most likely the Dispose or Close 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[^].


      www.troschuetz.de

      1 Reply Last reply
      0
      • M mitreviper

        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)

        N Offline
        N Offline
        Nnamdi Onyeyiri
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • M mitreviper

          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)

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

          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.

          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