Gdi objects and memory leak

Discussion in 'Microsoft Dot NET Framework' started by Duff, Dec 2, 2005.

  1. Duff

    Duff Guest

    Hi all,
    I am facing some serious issues with an application written in c#. I noticed
    (using Task Manager) that the number of GDI objects is increasing during the
    use of the application which crashes. I don't know how to locate the problem
    in the application and have several questions:
    - what does represent the number of GDI objects shown in the task manager?
    - how can I locate the unused gdi objects and destroy them?
    - can this be related to the fact that classes belonging to System.Drawing
    causes memory leaks?

    Please help, I have to solve that issue and it makes me crazy!!!!

    Thx
     
    Duff, Dec 2, 2005
    #1
    1. Advertisements

  2. Duff

    Guest Guest

    Are you sure you are looking at a memory leak? Are you consequently disposing
    the brushes, images and what more using either the Dispose method or the
    using statement?

    Are you using GDI objects yourself? In that case, are you implementing the
    IDisposable interface and releasing the GDI handles appropriately?

    "Duff" wrote:

    > Hi all,
    > I am facing some serious issues with an application written in c#. I noticed
    > (using Task Manager) that the number of GDI objects is increasing during the
    > use of the application which crashes. I don't know how to locate the problem
    > in the application and have several questions:
    > - what does represent the number of GDI objects shown in the task manager?
    > - how can I locate the unused gdi objects and destroy them?
    > - can this be related to the fact that classes belonging to System.Drawing
    > causes memory leaks?
    >
    > Please help, I have to solve that issue and it makes me crazy!!!!
    >
    > Thx
    >
    >
    >
     
    Guest, Dec 3, 2005
    #2
    1. Advertisements

  3. Duff wrote:
    > Hi all,
    > I am facing some serious issues with an application written in c#. I
    > noticed (using Task Manager) that the number of GDI objects is
    > increasing during the use of the application which crashes. I don't
    > know how to locate the problem in the application and have several
    > questions: - what does represent the number of GDI objects shown in
    > the task
    > manager?


    They are handles to things like unmanaged pens and brushes.

    > - how can I locate the unused gdi objects and destroy them?


    Whenever you create a System.Drawing.Pen or Brush make sure that you
    call Dispose on the object after you have finished using it. *However*,
    if you use a Pen from Pens or a Brush from Brushes *do not* dispose it.
    The reason is that Pens and Brushes contain static members which are
    created the first time they are accessed, if you Dispose such a member,
    the actual object will be disposed, but there will be an invalid
    reference in the static member, and hence when you access it you'll get
    an object disposed exception. This might be a cause of your 'leak' if
    you use lots of different brushes or pens through Brushes and Pens.

    > - can this be related to the fact that classes belonging to
    > System.Drawing causes memory leaks?


    I don't think so. Check all the code that creates Pen and Brush objects
    and dispose them correctly. Pay particular attention to loops:

    for (int i = 0; i < 100; i++)
    {
    Pen p = new Pen(Color.Black);
    g.DrawLine(p, x1, y1, x2, y2);
    }

    Here, 100 pens are created and are not disposed. If you have code like
    this take the declaration of the pen out of the loop and dispose it
    after the loop:

    Pen p = new Pen(Color.Black);
    for (int i = 0; i < 100; i++)
    {
    g.DrawLine(p, x1, y1, x2, y2);
    }
    p.Dispose();

    Richard
    --
    http://www.grimes.demon.co.uk/workshops/fusionWS.htm
    http://www.grimes.demon.co.uk/workshops/securityWS.htm
     
    Richard Grimes, Dec 3, 2005
    #3
  4. Duff

    Duff Guest

    Okay...
    Although I'm convinced my code is 'clean' (IDisposable implementation,
    etc...) I will take another look at that.
    But I tried to locate the problem using a profiler (.Net Memory Profiler
    v.2.6) and noticed that after 50 minutes of use I got about 10 000
    'undisposed' Bitmap class instances. That's why I was expected
    System.Drawing to cause memory leaks....

    To be continued...

    Thanks
     
    Duff, Dec 3, 2005
    #4
  5. Duff

    Guest Guest

    A great resource on this particular matter is 'Designing for a memory managed
    world':

    http://msdn.microsoft.com/netframework/programming/classlibraries/managedmemory/


    "Duff" wrote:

    > Hi all,
    > I am facing some serious issues with an application written in c#. I noticed
    > (using Task Manager) that the number of GDI objects is increasing during the
    > use of the application which crashes. I don't know how to locate the problem
    > in the application and have several questions:
    > - what does represent the number of GDI objects shown in the task manager?
    > - how can I locate the unused gdi objects and destroy them?
    > - can this be related to the fact that classes belonging to System.Drawing
    > causes memory leaks?
    >
    > Please help, I have to solve that issue and it makes me crazy!!!!
    >
    > Thx
    >
    >
    >
     
    Guest, Dec 3, 2005
    #5
  6. Duff

    Duff Guest

    Thanks for your usefull help.
    I located the issue which actually was a lack of GDI objects disposing.

    Thanks again.
    Duff
     
    Duff, Dec 5, 2005
    #6
  7. Duff

    Guest Guest

    Hey, that's great! Thanks for letting us know!

    "Duff" wrote:

    > Thanks for your usefull help.
    > I located the issue which actually was a lack of GDI objects disposing.
    >
    > Thanks again.
    > Duff
    >
    >
    >
     
    Guest, Dec 6, 2005
    #7
  8. Duff

    Strato

    Joined:
    May 8, 2012
    Messages:
    1
    Likes Received:
    0
    I usually use deleaker or similar debugger for GDI leaks in such cases... :bow:

    Your nick - is XSS? :lol: :fool:
     
    Strato, May 8, 2012
    #8
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Calm Interop

    Memory Leak with Unmanaged Objects

    Calm Interop, Sep 11, 2003, in forum: Microsoft Dot NET Framework
    Replies:
    0
    Views:
    287
    Calm Interop
    Sep 11, 2003
  2. Guest

    Problem: too many GDI objects in memory

    Guest, Jan 13, 2004, in forum: Microsoft Dot NET Framework
    Replies:
    1
    Views:
    694
    Nick Wienholt
    Jan 15, 2004
  3. Guest

    listview, gdi objects, GC

    Guest, Feb 22, 2005, in forum: Microsoft Dot NET Framework
    Replies:
    2
    Views:
    240
    Guest
    Feb 24, 2005
  4. Robin van de Water

    GDI+ is not properly initialized (internal GDI+ error).

    Robin van de Water, Jan 11, 2007, in forum: Microsoft Dot NET Framework
    Replies:
    0
    Views:
    356
    Robin van de Water
    Jan 11, 2007
  5. Jonathan Boivin

    Problems with GDI objects

    Jonathan Boivin, Nov 8, 2007, in forum: Microsoft Dot NET Framework
    Replies:
    0
    Views:
    251
    Jonathan Boivin
    Nov 8, 2007
Loading...

Share This Page