In the last months, I have had two or three reports of the users of my MZ-Tools extension about this exception:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Loading of the ImageList did not succeed. at System.Windows.Forms.ImageListStreamer..ctor(SerializationInfo info, StreamingContext context) --- End of inner exception stack trace ---
With a full trace that is included in this StackOverflow question with the same problem.
Incidentally, I had seen that exception very sporadically in the last tests of a session of integration tests of MZ-Tools.
For Visual Studio, MZ-Tools has more than 3,000 integration tests (most of them reproduce user actions such as setting options, executing commands and getting results in code or in treeviews, etc.). It is my experience that no version of Visual Studio has been able to run all the tests without crashing due to out of memory errors, etc. so I tend to run them in sets of 500-800 tests, restarting Visual Studio after each session. In this scenario, I have never seen the exception above.
For VB6 and VBA, on the other hand, there are “only” 1,500+ integration tests, and both IDEs are very capable of running all of them in a single session (and much faster than Visual Studio). In this case, I have seen the exception in the last tests but very rarely. So, I finally decided to investigate this issue.
There is little information on the web about this exception with the ImageList. Some old MSDN KB articles mention problems with the ImageList and leaks:
FIX: ImageList Class Leaks GDI objects (for .NET Framework 1.0)
Memory usage increases for a .NET Framework 2.0-based application when you use the ImageList.ImageCollection.Item property to obtain an image or when you call the ImageList.ImageCollection.GetEnumerator method.
I am using .NET Framework 2.0 but it is fully patched. Since the problem could be related to GDI objects leaks, I used theTask Manager to check it:
To add the GDI Objects column you need to right-click on the column header and then select it on the list:
Then I ran the 1,500+ integration tests in Excel while watching the GDI Objects counter and, to my surprise, the counter increased and increased, never releasing objects. So, there is a GDI object leak somewhere that I will investigate in the next days. I don’t know yet if this is the same cause for the users of my extension (it could be that they use the IDEs for very long periods of time without restarting) but certainly I have now a a GDI object leak to find.