Question

I have this problem where I have a very large form (winforms) that is loaded through the Activation.CreateInstance(Type) method, because the loading container is used for different forms. It opens fine the first time, but if I close it and then reopen it, it will always crash halfway through the InitializeComponent() method, where it 'jumps' to the Dispose() method, crashing on a NullReferenceException.

My current theory is that the Dispose() call is from the previous instance of the form that did not have the time to finish and is now trying to dispose of the current form. (does that make any sense?) On the other hand, I think that maybe it's the Activation.CreateInstance() that is causing the problem, but it's the first time I see that class so I'm not so sure about that either.

I did a test earlier today while debugging, and if I waited a few seconds on each line in the crashing InitializeComponent(), I got through without error.

Was it helpful?

Solution

Finally I found the origin of the bug : the loading form has hundreds of grids with little toolbars each having 5-6 icons on them. And, the resource manager being a little daft, instead of reusing the same icon each time (the toolbars are instances of the same usercontrol), it instantiates a new bitmap each time, running out-of-memory. But, instead of failing with an obvious error, it squirrels it away and starts disposing of the calling objects, therefore disposing incompletely created objects. So the solution here is to make sure to load images only once when using the resource manager. Not only will it resolve the crash, it also loads faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top