I read a lot about this since I was asked to fix a C # application that has memory leak problems, but I could not find an answer to these 2 problems:
Consider the following code:
private static ArrayList list = new ArrayList(); public void Function() { list.add(object1); list.add(object2);
Since the list was not cleared before creating a new one, will it generate some kind of garbage that will not be released after the static list is placed?
The second problem concerns Form.Dispose (). I see that many of the controls available in the designer’s view (i.e. Labels, picture boxes) require removal. It seems that calling Dispose () on the form will cause all these types of controls to be deleted as well (correct me if I'm wrong), which is odd, as the designer adds redundant void Dispose (bool disposing) , which doesn't do this. I assume this happens in the void Dispose (bool disposing) method of the Form base class.
The problem with the above is that it is not very clear to me what I need to do to ensure that all Form resources are correctly located. I don’t understand how Form knows which objects it needs to dispose of. For example, if in my form I have a field that is an IDisposable custom object, will the Form know what it needs to be disposed of? Or should I add the code needed to self-release the object?
Also, if I need to add code to place certain objects, then how can I deal with the fact that the designer has already overcome the void Dispose (bool disposing) method ? Should I edit the code created by the constructor or is there a cleaner way to do this?
I hope this should not have been embarrassing, it is a little difficult to explain. Thanks
source share