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:
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 03:18 PM.
"My software doesn't have bugs, it just develops random features."
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:
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
Bookmarks