Solving Memory Leaks
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Solving Memory Leaks

  1. #1
    Join Date
    Mar 2006
    Posts
    94

    Question Solving Memory Leaks

    I have a program that runs C++ for its behind-the-scenes functional operations and runs C# for its GUI portion, however, the C# GUI executable has a memory leak issue and I'm trying to track it down. The memory of the program gradually increases over time, eventually handicapping the PC. I ran .NET Memory Profiler and the items that used the most memory were:

    System.WeakReference
    System.Hastable.bucket[]
    System.Windows.Forms
    GCHandle

    Since these are system items, this leads me to believe that I'm using them incorrectly in my code. That being said, here are some samples of code which I believe to be suspect for memory leaks, but I'm not sure if there is a way or how to fix them.

    Code:
    public class Gui
    {
           // Variables
           private MyFormClass myFormVariable = null;
           private System.Collections.ArrayList myArrayList = new System.Collections.ArrayList();
    
           // Constructor
           public Gui()
           {
                  System.Collections.ArrayList localArrayList = new System.Collections.ArrayList(myArrayList);
    
                  object myObject = new object();
                 
                  myArrayList.Add(object);
    
                  myFormVariable = new MyFormClass();
    
                  MyMethod();
                  
           }
    
            private void MyMethod()
            {
                   System.Drawing.Point myPoint = new System.Drawing.Point (34, 3);
                   
                   char[] c = new char[2];
                   c[0] = 'c';
    
                   if (myFormVariable != null)
                   {
                           myFormVariable.Location = myPoint;
                           myFormVariable.Name = new string (c);
                   }
            }
    }
    Will any of these variable initializations/usages cause memory leaks? If so, how do I fix it? Of course I just gave a sample code snippet...these types of initializations are all over the code I'm working with.
    Last edited by Ciralia; July 11th, 2011 at 04:18 PM.
    "My software doesn't have bugs, it just develops random features."

  2. #2
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Solving Memory Leaks

    Are you absolutely sure the memory leak is not on the C++-side of your app? Since it is more likely.

  3. #3
    Join Date
    Mar 2006
    Posts
    94

    Re: Solving Memory Leaks

    Well the app has 4 executables...3 of which are C++ and 1 which is C# and the one that is C# is the one that is having the memory problem.
    "My software doesn't have bugs, it just develops random features."

  4. #4
    Join Date
    Jun 2006
    Posts
    644

    Re: Solving Memory Leaks

    Hi,
    Your information is incomplete atleast for me. However, from whatever you have posted, I can tell you that there is probably a resource leak in the form of GDI object. May be some sort of object belonging to System.Drawing or similar and which is not freed. The reason for that is the lines in your post:
    Code:
    System.WeakReference
    System.Hastable.bucket[]
    System.Windows.Forms
    GCHandle
    A weakReference is a class that is used by Garbage Collector (GC) to maintain a track of objects which might be referenced even after they are listed as candidates for collection after being dereferenced. When GC tries to get a handle to the resource, it fails. Also, when you say that the problem is in the C# application, please make sure that you are performing marshalling correctly. If you can look at the MSIL code generated using ILDASM, even better. Also, are the Forms and UI a part of the managed code?
    May be you can now look for other sources that can cause the error and post the info here...so that some one will gladly help you
    Good luck!
    Bhushan

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

    Re: Solving Memory Leaks

    Quote Originally Posted by Ciralia View Post
    Well the app has 4 executables...3 of which are C++ and 1 which is C# and the one that is C# is the one that is having the memory problem.
    From your 1st post, it sounded like the C# app calls native dlls? Is this not the case?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center